• Custom Plugin
  • |
  • PHP
  • |
  • Robolly
  • |
  • WordPress
  • |
  • WPCodebox

Auto Featured Image by Post Types Using Robolly API and Templates

This snippet uses Robolly Templates and API to generate Featured Images for a Specific Post Type. It has WP Admin page inside Settings > AFI—Robolly.

A screenshot of the AFI Robolly Settings page in WordPress displays places where you can enter the Robolly API key and template IDs to automatically create featured images. There are settings for various post types and links to more information about Robolly and donations.

Conditions:

  1. Requires Robolly Template to be done
  2. Requires Robolly API key
  3. A post with a published status, only if there is NO featured image already attached
  4. Setup credentials in Settings > AFI — Robolly

Operation:

  1. Set up a Template in Robolly
  2. Copy the Template ID and Robolly API Key
  3. Set up the preferences
  4. Now go to a published post or page
  5. Make sure there are no existing-featured images is set to the post
  6. Press Save — wait for it to work
  7. Refresh the post editor to see the new featured image
The Robolly screen shows a sidebar with options such as My templates, Datasets, and others. On the right side, you can see a preview of a template with example text. This illustrates how the Robolly API easily adds automatic featured images to different post types.
Setup templates in Robolly
Ruhani Rabins promotional banner is advertising a free service for tracking URLs and creating status pages. It works smoothly with the Robolly API. The banner features a gradient background that goes from orange to dark gray, has bold white text, and includes an arrow pointing to the banner on the webpage.
Once published, the featured image is created automatically using the Robolly API and Template

Works with:

  1. WPCodeBox
  2. FluentSnippets
  3. Other Code Snippets Plugins

Copy the code and paste it inside your favorite code-snippet plugin.

Type: PHP

<?php
/*
Plugin Name: AFI - Robolly
Description: Automatically generates and assigns dynamic featured images using Robolly API
Version: 1.0
Release: Production
Author: Ruhani Rabin
URL: https://www.ruhanirabin.com

Licence: MIT

Copyright 2024 Ruhani Rabin https://www.ruhanirabin.com

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in 
the Software without restriction, including without limitation the rights to 
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to 
do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

if (!defined('ABSPATH')) exit;

class AFI_Robolly_Generator {
    private $api_base_url = 'https://api.robolly.com/templates/';
    private $max_attempts = 3;
    private $plugin_options_key = 'afi_robolly_settings';

    public function __construct() {
        add_action('save_post', array($this, 'handle_post_save'), 10, 1);
        add_action('admin_menu', array($this, 'add_admin_menu'));
        add_action('admin_init', array($this, 'register_settings'));
        
        if (false === get_option($this->plugin_options_key)) {
            add_option($this->plugin_options_key, $this->get_default_settings());
        }
    }

    public function add_admin_menu() {
        add_options_page(
            'AFI Robolly Settings',
            'AFI Robolly',
            'manage_options',
            'afi-robolly-settings',
            array($this, 'render_settings_page')
        );
    }

    public function register_settings() {
        register_setting(
            'afi_robolly_settings',
            $this->plugin_options_key,
            array($this, 'sanitize_settings')
        );

        add_settings_section(
            'afi_api_section',
            'API Settings',
            array($this, 'api_section_callback'),
            'afi-robolly-settings'
        );

        add_settings_field(
            'afi_api_key',
            'Robolly API Key',
            array($this, 'api_key_field_callback'),
            'afi-robolly-settings',
            'afi_api_section'
        );

        add_settings_section(
            'afi_main_section',
            'Post Type Settings',
            array($this, 'section_callback'),
            'afi-robolly-settings'
        );

        foreach (get_post_types(['public' => true], 'objects') as $post_type) {
            add_settings_field(
                'afi_' . $post_type->name,
                $post_type->labels->singular_name,
                array($this, 'post_type_field_callback'),
                'afi-robolly-settings',
                'afi_main_section',
                array('post_type' => $post_type->name)
            );
        }
    }

    public function api_section_callback() {
        echo '<p>Enter your Robolly API Key. This is required for the plugin to function.</p>';
    }

    public function api_key_field_callback() {
        $options = get_option($this->plugin_options_key);
        $api_key = isset($options['api_key']) ? $options['api_key'] : '';
        ?>
        <input type="text" 
               name="<?php echo esc_attr($this->plugin_options_key); ?>[api_key]" 
               value="<?php echo esc_attr($api_key); ?>"
               class="regular-text"
               required>
        <?php
    }

    public function section_callback() {
        echo '<p>Configure which post types should have auto-generated featured images and their respective template IDs using Robolly.</p>';
        echo '<p><strong>Note:</strong> Template IDs are specific to your <strong><a href="https://robolly.com/dashboard" target="blank">Robolly</a></strong> account. Make sure to use valid <strong>template IDs</strong> from an active account</p>';
        echo '<p><strong>Plugin by <a href="https://www.ruhanirabin.com" target="blank">RuhaniRabin.com</a></strong>(MIT license). <a href="https://paypal.me/ruhanirabin" target="blank">Donate</a> if this helped you out.</p>';
    }

    public function post_type_field_callback($args) {
        $defaults = $this->get_default_settings();
        $options = get_option($this->plugin_options_key, $defaults);
        $post_type = $args['post_type'];
        
        $options[$post_type] = wp_parse_args($options[$post_type], array(
            'enabled' => false,
            'template_id' => ''
        ));
        
        $enabled = $options[$post_type]['enabled'];
        $template_id = $options[$post_type]['template_id'];
        ?>
        <div class="post-type-settings">
            <label>
                <input type="checkbox" 
                       name="<?php echo esc_attr($this->plugin_options_key); ?>[<?php echo esc_attr($post_type); ?>][enabled]" 
                       value="1" 
                       <?php checked($enabled, true); ?>>
                Enable auto featured image generation
            </label>
            <br>
            <label>
                Template ID:
                <input type="text" 
                       name="<?php echo esc_attr($this->plugin_options_key); ?>[<?php echo esc_attr($post_type); ?>][template_id]" 
                       value="<?php echo esc_attr($template_id); ?>"
                       placeholder="Enter Robolly template ID"
                       class="regular-text">
            </label>
        </div>
        <?php
    }

    public function render_settings_page() {
        if (!current_user_can('manage_options')) {
            return;
        }
        ?>
        <div class="wrap">
            <h1><?php echo esc_html(get_admin_page_title()); ?></h1>
            <form action="options.php" method="post">
                <?php
                settings_fields('afi_robolly_settings');
                do_settings_sections('afi-robolly-settings');
                submit_button('Save Settings');
                ?>
            </form>
        </div>

        <style>
            .post-type-settings {
                margin-bottom: 15px;
                padding: 10px;
                background: #fff;
                border: 1px solid #ccc;
                border-radius: 4px;
            }
            .post-type-settings input[type="text"] {
                margin-top: 5px;
                width: 100%;
                max-width: 400px;
            }
            .post-type-settings label {
                display: block;
                margin-bottom: 5px;
            }
        </style>
        <?php
    }

    private function get_default_settings() {
        $defaults = array('api_key' => '');
        $post_types = get_post_types(['public' => true], 'names');
        
        foreach ($post_types as $post_type) {
            $defaults[$post_type] = array(
                'enabled' => false,
                'template_id' => ''
            );
        }
        
        return $defaults;
    }

    public function sanitize_settings($input) {
        $sanitized_input = array();
        
        $sanitized_input['api_key'] = sanitize_text_field($input['api_key']);

        foreach (get_post_types(['public' => true], 'names') as $post_type) {
            $sanitized_input[$post_type] = array(
                'enabled' => isset($input[$post_type]['enabled']) ? true : false,
                'template_id' => isset($input[$post_type]['template_id']) 
                    ? sanitize_text_field($input[$post_type]['template_id']) 
                    : ''
            );
        }
        
        return $sanitized_input;
    }

    private function is_post_type_supported($post_type) {
        $options = get_option($this->plugin_options_key, array());
        return isset($options[$post_type]['enabled']) && 
               $options[$post_type]['enabled'] && 
               !empty($options[$post_type]['template_id']);
    }

    private function get_template_id($post_type) {
        $options = get_option($this->plugin_options_key, array());
        return isset($options[$post_type]['template_id']) 
            ? $options[$post_type]['template_id'] 
            : false;
    }

    private function get_final_image_url($api_url, $api_key) {
        for ($attempt = 1; $attempt <= $this->max_attempts; $attempt++) {
            $response = wp_remote_get($api_url, array(
                'timeout' => 30,
                'redirection' => 5,
                'headers' => array(
                    'Accept' => 'application/json',
                    'Authorization' => 'Bearer ' . $api_key
                ),
                'sslverify' => true
            ));

            if (is_wp_error($response)) {
                error_log('AFI Robolly: API request failed on attempt ' . $attempt);
                continue;
            }

            $body = wp_remote_retrieve_body($response);
            $json_data = json_decode($body, true);
            
            if (json_last_error() === JSON_ERROR_NONE && isset($json_data['url'])) {
                return $json_data['url'];
            }

            if ($attempt < $this->max_attempts) {
                sleep(2);
            }
        }

        return false;
    }

    private function clean_title($title) {
        $clean_title = html_entity_decode($title, ENT_QUOTES | ENT_HTML5);
        $clean_title = preg_replace('/&#?[a-z0-9]{2,8};/i', '', $clean_title);
        return trim(sanitize_text_field($clean_title));
    }

    private function process_attachment($file_array, $post_id, $post) {
        $attachment_id = media_handle_sideload($file_array, $post_id);

        if (is_wp_error($attachment_id)) {
            error_log('AFI Robolly: Failed to create attachment - ' . $attachment_id->get_error_message());
            return false;
        }

        $post_title = $post->post_title;
        $post_excerpt = has_excerpt($post->ID) ? get_the_excerpt($post) : $post_title;
        
        $attachment_data = array(
            'ID' => $attachment_id,
            'post_title' => $post_title,
            'post_excerpt' => $post_excerpt,
            'post_content' => $post_excerpt,
        );
        wp_update_post($attachment_data);
        update_post_meta($attachment_id, '_wp_attachment_image_alt', $post_title);

        return $attachment_id;
    }

    public function handle_post_save($post_id) {
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return;
        }

        if (defined('DOING_AJAX') && DOING_AJAX) {
            return;
        }

        $post = get_post($post_id);
        if (!$post || !$this->is_post_type_supported($post->post_type) || 
            $post->post_status !== 'publish' || has_post_thumbnail($post_id)) {
            return;
        }

        $options = get_option($this->plugin_options_key);
        $api_key = isset($options['api_key']) ? $options['api_key'] : '';
        if (empty($api_key)) {
            error_log('AFI Robolly: API key is not set');
            return;
        }

        $template_id = $this->get_template_id($post->post_type);
        if (!$template_id) {
            return;
        }

        $clean_title = $this->clean_title($post->post_title);
        $api_url = sprintf(
            '%s%s/render?title=%s&json=1',
            $this->api_base_url,
            $template_id,
            rawurlencode($clean_title)
        );

        $image_url = $this->get_final_image_url($api_url, $api_key);
        if (!$image_url) {
            error_log('AFI Robolly: Failed to get image URL for post ' . $post_id);
            return;
        }

        $temp_file = download_url($image_url);
        if (is_wp_error($temp_file)) {
            error_log('AFI Robolly: Failed to download image - ' . $temp_file->get_error_message());
            return;
        }

        $file_array = array(
            'name' => sanitize_title($post->post_name) . '-' . time() . '.jpg',
            'tmp_name' => $temp_file
        );

        $attachment_id = $this->process_attachment($file_array, $post_id, $post);
        if ($attachment_id) {
            set_post_thumbnail($post_id, $attachment_id);
        }

        @unlink($temp_file);
    }
}

new AFI_Robolly_Generator();


More Codes

How to Add “NEW”, “UPDATED”, and “OUTDATED” Badges to Your WordPress Posts

Enhance your WordPress posts with “NEW,” “UPDATED,” and “OUTDATED” badges! Keep your content fresh and organized for better user experience.

How to Add Source Code Highlights and a Copy Button in WordPress

“Learn how to add source code highlights and a ‘Copy Code’ button to your WordPress site without using plugins! Improve functionality and aesthetics today.”

Shell/Bash Script Migrating WordPress – Find and Replace all Instance of Domain Name in WordPress or MySQL Database

Automate WordPress Domain Migration with our Shell Script. Easily Find & Replace all instances of your old domain in the MySQL database. Try it now!