I am trying to implement auto set featured image from my post content and if there is no image in the post content then set a default featured image automatically..

I have tried different code from different source but nothing seems to be working. My post content image is set from other source and these are not hosted in my site wp media library. Could this be a problem ?

I had one code that set featured image once the post is saved and these images was in the Wp library.. But my images are from other third party soruce and set it like this way below

<img src="google.com/j.jpg" /> or like that.. These images are not hosted in my wp media library.

You will have a clear Idea if you see this

enter image description here

So finally what I want is that set a featured image automatically from post content for new created posts and for old posts as well these are already there.. If there is no image in the Post content then just simply set a default image which can be static source..

Thanks in Advance

2 Answers 2


Ok guys.. I got it finally working with my below code.

function wpse55748_filter_post_thumbnail_html( $html ) {

    // If there is no post thumbnail,
    // Return a default image
    global $post;
    $pID = $post->ID;
    $thumb = 'large';
    $imgsrc = FALSE;
    if (has_post_thumbnail()) {
        $imgsrc = wp_get_attachment_image_src(get_post_thumbnail_id($pID),$thumb);
        $imgsrc = $imgsrc[0];
    } elseif ($postimages = get_children("post_parent=$pID&post_type=attachment&post_mime_type=image&numberposts=0")) {
        foreach($postimages as $postimage) {
            $imgsrc = wp_get_attachment_image_src($postimage->ID, $thumb);
            $imgsrc = $imgsrc[0];
    } elseif (preg_match('/<img [^>]*src=["|\']([^"|\']+)/i', get_the_content(), $match) != FALSE) {
        $imgsrc = $match[1];
    if($imgsrc) {
        $imgsrc = '<img src="'.$imgsrc.'" alt="'.get_the_title().'" class="summary-image" />';
        $html = $imgsrc;

    if ( '' == $html ) {
        return '<img src="' . get_template_directory_uri() . '/images/default-featured-image.png" />';
    // Else, return the post thumbnail
    return $html;
add_filter( 'post_thumbnail_html', 'wpse55748_filter_post_thumbnail_html' );

Here I first search the post content with regex if the content have any kind if


I guess this is something you're looking for, first it checks if post thumbnail is present, otherwise display a fallback image.

<?php if(has_post_thumbnail()) : ?>
    <?php the_post_thumanail(); ?>
<?php else : ?>
    <img src="http://via.placeholder.com/1350x450"/>
<?php endif; ?>

Note: Replace the image src on the else statement to your static resource.

  • I have this already and that's a part of this question. Please read the question in details first.. I want to set default featured image from My post content for older post and my post content images are from a third party image source.. Oct 20, 2017 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.