Site logo

Archived topic

Default / Fallback featured image

22 replies · Started by Dan on February 25, 2018

Viewing posts 16–23 of 23

That plugin is using the post_thumbnail_html filter we tried above.

We could try tweaking that function again:

add_filter( 'post_thumbnail_html', 'tu_post_thumbnail_fallback', 20, 5 );
function tu_post_thumbnail_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
    if ( empty( $post_thumbnail_id ) ) {
        $html = '<img src="URL TO YOUR FALLBACK IMAGE" alt="" />';
    }

    return $html;
}

Hi Tom,
Wanted to know if there's an update regarding this.

Thanks
Dan

I assume my above tweak didn't work?

no, tried it again on a different site and it doesn't show either.
It's like it's ignoring either the filter or the function.

I'll put aside some time tomorrow to test this thoroughly - will be back with working code :)

Looking at this more. The code I would give you would be directly taken from the plugin I mentioned earlier, which seems like a bit of a waste.

What's wrong with using the plugin again exactly?

Hi Tom
Thanks for taking the time to look into this.
I have 2 concerns:
1. why isn't the core function for detecting and placing a default featured image not working? is that a sign for maybe something else not functioning correctly?
2. I usually prefer using vanilla code rather than a plugin for the obvious reasons.

Thanks
Dan

1. GP checks for has_post_thumbnail(), which checks the post meta for an ID. Since that doesn't exist (even if we filter the HTML), nothing is returned.

2. A plugin is just code. That particular plugin is very small and only does a couple things. It's also been updated recently. The plugin does 3 things:

1. Adds a field for you to upload an image.
2. Filters the post meta and adds the ID of that image.
3. Filters the featured image HTML.

So the code I would give you would be their code from #2 and #3. We can do that if you'd like, but I'm not sure I see the benefit. Not all plugins are evil ;)

This archived topic is closed to new replies.