• You MUST read the Babiato Rules before making your first post otherwise you may get permanent warning points or a permanent Ban.

    Our resources on Babiato Forum are CLEAN and SAFE. So you can use them for development and testing purposes. If your are on Windows and have an antivirus that alerts you about a possible infection: Know it's a false positive because all scripts are double checked by our experts. We advise you to add Babiato to trusted sites/sources or disable your antivirus momentarily while downloading a resource. "Enjoy your presence on Babiato"

First Image as Featured ... when featured was not set

Funesto

Member
May 11, 2019
40
10
8
Hello!
Is there a plug-in setting first article image as featured ... but ONLY if a feauted was not manually set?

Sometimes I forget to set a featured image and in that case I'd like the plug in to do it for me, picking from the first image in article. But this only if I did not already set it by myself!

Any suggetsions?

Thank you so much!

G.
 
You can add this code in your theme's functions.php file instead of a plugin:
PHP:
function set_first_image_as_featured($post_id) {
    $post = get_post($post_id);

    // Check if a featured image is already set
    if (has_post_thumbnail($post_id)) {
        return;
    }

    // Get the first image from the post content
    $first_image = '';
    $matches = array();
    preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $post->post_content, $matches);
    if (isset($matches['src'])) {
        $first_image = $matches['src'];
    }

    // If a first image is found, set it as the featured image
    if (!empty($first_image)) {
        $attachment_id = attachment_url_to_postid($first_image);
        if ($attachment_id) {
            set_post_thumbnail($post_id, $attachment_id);
        }
    }
}

add_action('save_post', 'set_first_image_as_featured');

And for existing posts, you can add this one time code and after that refresh your wp dashboard and then remove this code:

PHP:
function set_featured_images_for_existing_posts() {
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => -1, // set the number of posts per page to -1 to get all posts
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            set_first_image_as_featured(get_the_ID());
        }
        wp_reset_postdata();
    }
}
add_action('init', 'set_featured_images_for_existing_posts');
 
  • Love
Reactions: DenSouLew
Hello!
Is there a plug-in setting first article image as featured ... but ONLY if a feauted was not manually set?

Sometimes I forget to set a featured image and in that case I'd like the plug in to do it for me, picking from the first image in article. But this only if I did not already set it by myself!

Any suggetsions?

Thank you so much!

G.
Hey Thank you so much! :)
 
  • Love
Reactions: mrskt
Becouse of my VERY CHEAP hosting I had to remove the code for existing post ... It was always "Error 500". Anyway .. Now I am sure my next post won't miss featured image! Thanks again! :)
 
  • Like
Reactions: mrskt
AdBlock Detected

We get it, advertisements are annoying!

However in order to keep our huge array of resources free of charge we need to generate income from ads so to use the site you will need to turn off your adblocker.

If you'd like to have an ad free experience you can become a Babiato Lover by donating as little as $5 per month. Click on the Donate menu tab for more info.

I've Disabled AdBlock