Fix Mixed-Mode YouTube Embeds in WordPress

Are you attempting to embed YouTube videos in a secure (https) WordPress site?  Well, browsers should be blocking that content since the videos are not coming in over a secure channel.

A consequence is that some YouTube videos won’t display, and an error won’t be displayed–which is annoying.

Here is a brief snippet that can be added to your functions.php file to fix the problem once and for all.

function youtube_the_content($content) {
	$search = array("http://www.youtube.com", "http://youtube.com");

	return str_replace($search, "https://www.youtube.com", $content);
}

add_filter( 'the_content', 'youtube_the_content' );

The line beginning with “$search” specifies that we are searching for non-secure links to YouTube, and the return line combs through the post, and fixes the links so the embeds display properly.  It’s a simple fix, based on WordPress’ powerful filter hook system.

Leave a Reply

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