Hack: Fixing a YouTube Thumbnail Bug

So I came across an interesting bug when I noticed that one of the embedded YouTube videos right here at this site wasn’t displaying on the front page. It looked fine in the post, but the front page just showed YouTube’s missing thumbnail image instead of the real thumbnail.

A quick look at the HTML code showed that the YouTube ID for the video included some dash characters. But the thumbnail image displayed on the front page converted those to \u2003d. That’s fine for displaying text, but it messes up the URL for the image.

So I had to scramble to find a fix that didn’t involve editing the theme directly. I could use the_content filter and str_replace to change any regular text, but it wouldn’t work on the image. It just stayed \u2003d no matter what I did.

It took a few false starts, but I eventually discovered the post_thumbnail_html filter. That solved it. Here is my code, which I added to the functions.php file.

add_filter('post_thumbnail_html', 'wpfilter_fix_youtube_dash_bug', 10, 3);
function wpfilter_fix_youtube_dash_bug($html, $post_id, $post_image_id) {
	$html = str_replace('\u002d','-',$html);
	return $html;
}

I wonder if any other characters besides the dash were affected. If so, and I come across them, it will be easy to add to this filter.

Leave a Reply

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