Automatically add a featured Image to all posts, that have none

By using this code, your WordPress Website will automatically add a provided Image URL as the featured Image to all posts, that have currently no featured Image selected.

function set_default_featured_image($post_id) {
    // Check if the post already has a featured image
    if (!has_post_thumbnail($post_id)) {
        // URL of the default image
        $default_image_url = 'https://dev.gooloo.de/wp-content/uploads/2024/09/gooloo_1200x728.png';
        
        // Get the ID of the default image (upload it to the media library if it doesn't exist)
        $default_image_id = attachment_url_to_postid($default_image_url);
        
        if (!$default_image_id) {
            // If the image doesn't exist in the media library, download and attach it
            $default_image_id = media_sideload_image($default_image_url, $post_id, '', 'id');
        }
        
        // Set the default image as the featured image
        if (!is_wp_error($default_image_id)) {
            set_post_thumbnail($post_id, $default_image_id);
        }
    }
}

// Hook the function to run when a post is saved or updated
add_action('save_post', 'set_default_featured_image');

// Hook the function to run when a post is created or updated via the REST API
add_action('rest_after_insert_post', 'set_default_featured_image');

// Function to apply default featured image to existing posts
function apply_default_featured_image_to_existing_posts() {
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'meta_query' => array(
            array(
                'key' => '_thumbnail_id',
                'compare' => 'NOT EXISTS'
            ),
        )
    );

    $posts_without_thumbnail = new WP_Query($args);

    if ($posts_without_thumbnail->have_posts()) {
        while ($posts_without_thumbnail->have_posts()) {
            $posts_without_thumbnail->the_post();
            set_default_featured_image(get_the_ID());
        }
    }

    wp_reset_postdata();
}

// Add an admin menu item to trigger the process
function add_apply_default_image_menu() {
    add_management_page(
        'Apply Default Featured Image',
        'Apply Default Image',
        'manage_options',
        'apply-default-featured-image',
        'apply_default_featured_image_page'
    );
}
add_action('admin_menu', 'add_apply_default_image_menu');

// Callback function for the admin page
function apply_default_featured_image_page() {
    if (isset($_POST['apply_default_image'])) {
        apply_default_featured_image_to_existing_posts();
        echo '<div class="updated"><p>Default featured image has been applied to existing posts without featured images.</p></div>';
    }
    ?>
    <div class="wrap">
        <h1>Apply Default Featured Image</h1>
        <form method="post">
            <?php wp_nonce_field('apply_default_image_action', 'apply_default_image_nonce'); ?>
            <p>Click the button below to apply the default featured image to all existing published posts that don't have a featured image.</p>
            <input type="submit" name="apply_default_image" class="button button-primary" value="Apply Default Image">
        </form>
    </div>
    <?php
}

Replace the URL

https://dev.gooloo.de/wp-content/uploads/2024/09/gooloo_1200x728.png

with your desired Image URL.

This snippet does the following:

  1. It defines a function called set_default_featured_image that checks if a post has a featured image.
  2. If the post doesn’t have a featured image, it uses the specified URL as the default image.
  3. It checks if the default image already exists in the media library. If not, it downloads and attaches the image.
  4. Finally, it sets the default image as the featured image for the post.
  5. The function is hooked to both save_post and rest_after_insert_post actions to cover both traditional editing and REST API updates.

Updated:

  1. A new function apply_default_featured_image_to_existing_posts() that queries all published posts without a featured image and applies the default image to them.
  2. An admin menu item under the “Tools” menu called “Apply Default Image”.
  3. An admin page with a button that, when clicked, triggers the process to apply the default featured image to existing posts.

To use this:

  1. Add this code to your theme’s functions.php file or create a custom plugin.
  2. Go to the WordPress admin panel and navigate to “Tools” > “Apply Default Image”.
  3. Click the “Apply Default Image” button to process all existing posts.
Apply Default Featured Image ‹ gooloo.de for Developers — WordPress 23.09.2024 19 09
Automatically add a featured Image to all posts, that have none

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *