Support » Plugin: Video Thumbnails » [Plugin: Video Thumbnails] Patch: Support https(a|vh),, private, and mass recreate deleted

  • Sutherland: Could you incorporate this into the next stable?

    Three steps copy-paste to add support for https, URLs, private posts and mass recreation of deleted thumbs and readds support for Smart Youtube and Youtube Lyte to Video Thumbnails 1.7.6.

    1. In video-thumbnails.php find from line 91 and change it from

    // Check to see if thumbnail has already been found
    	if( ($thumbnail_meta = get_post_meta($post_id, '_video_thumbnail', true)) != '' ) {


    // Check to see if thumbnail has already been found and still exists on our server as a file
    	if( ( ($thumbnail_meta = get_post_meta($post_id, '_video_thumbnail', true)) != '' ) && wp_remote_retrieve_response_code(wp_remote_head($thumbnail_meta)) === '200'  ) {

    2. Find from line 111 and change it from

    // Checks for a standard YouTube embed
    		preg_match('#<object[^>]+>.+?[ve]/([A-Za-z0-9\-_]+).+?</object>#s', $markup, $matches);
    		// More comprehensive search for YouTube embed, redundant but necessary until more testing is completed
    		if(!isset($matches[1])) {
    			preg_match('#[ve]/([A-Za-z0-9\-_]+)#s', $markup, $matches);
    		// Checks for YouTube iframe
    		if(!isset($matches[1])) {
    			preg_match('#[A-Za-z0-9\-_]+)#s', $markup, $matches);
    		// Checks for any YouTube URL
    		if(!isset($matches[1])) {
    			preg_match('#http://w?w?w?.?\?v=([A-Za-z0-9\-_]+)#s', $markup, $matches);


    // Checks for the old standard YouTube embed
    		preg_match('#<object[^>]+>.+?https?://[ve]/([A-Za-z0-9\-_]+).+?</object>#s', $markup, $matches);
    		// More comprehensive search for YouTube embed, redundant but necessary until more testing is completed
    		if(!isset($matches[1])) {
    			preg_match('#https?://[ve]/([A-Za-z0-9\-_]+)#s', $markup, $matches);
    		// Checks for YouTube iframe, the new standard since at least 2011
    		if(!isset($matches[1])) {
    			preg_match('#https?://[A-Za-z0-9\-_]+)#s', $markup, $matches);
    		// Checks for any YouTube URL. After http(s) support a or v for Youtube Lyte and v or vh for Smart Youtube plugin
    		if(!isset($matches[1])) {
    			preg_match('#(?:https?(?:a|vh?)?://)?(?:www\.)?\?v=([A-Za-z0-9\-_]+)#s', $markup, $matches);
    		// Checks for any shortened URL. After http(s) a or v for Youtube Lyte and v or vh for Smart Youtube plugin
    		if(!isset($matches[1])) {
    			preg_match('#(?:https?(?:a|vh?)?://)?[A-Za-z0-9\-_]+)#s', $markup, $matches);

    3. Find line 336 (331 beforer the above changes) and change it from

    if ( get_post_status() == 'publish') {


    if ( get_post_status() == 'publish' || get_post_status() == 'private' ) {

    Summary of changes in the second bit:
    – support added as a separate if statement
    https? in all URLs to support https
    – Added (?:a|vh?)? after https? to catch a, v or vh to support Youtube Lyte and Smart Youtube in the two catchalls
    – Enclosed the protocol prefix in both of them in non-capturing parentheses to match also without it
    – Changed w?w?w?.? to (?:www\.). This uses non-capturing parentheses and has the comma escaped. The previous one would have matched for example “” (notice double w & y) which isn’t desired
    – Updated comments to reflect the above and tell between the old and new standard Youtube embed

    The first bit checks the HTTP header to see if the thumbnail file still exists on the blog’s server. Need for this arose as the scan past posts function of Video Thumbnails didn’t retrieve new ones after mass deletion of the thumbs. There should be a smarter way to do this through the file system itself if someone wants to contribute it.

    Tested with iframe embeds and bare links that WordPress kindly autoembeds. Works like a charm with both. Also works with https, httpa, httpv, httpvh and their https counterparts with the bare URL and should thus address the issues mentioned in [Plugin: Video Thumbnails] We didn’t find a video thumbnail for this post.

    Doesn’t support Smart Youtube’s httpvp or httpvhp playlists. Couldn’t get any Youtube playlist embedding to work so that’ll be left for later. No updates to the support of any other services than Youtube except for the private post type. Another thing that could use contributions is thinking if some other post statuses than public and private should be supported from this list:

Viewing 15 replies - 1 through 15 (of 20 total)
  • Plugin Author Sutherland Boswell


    Thanks Daedalon, I’m gonna try to work these in as soon as I have a little time. It’s tough debugging all the issues with all the different hosting environments and video setups, so I really appreciate the help.

    Glad to contribute. The above should work without a hitch and works at my environment, but is of course worth testing in at least one more environment. It doesn’t yet improve the support of any other services besides Youtube as I don’t use them myself, but the changes are very straight-forward to implement for other services too. It’s just a case of changing http://www. to (?:https?(?:a|vh?)?://)?(?:www\.)? where appropriate.

    This does add support for some things that might not yet be possible, but there doesn’t seem to be a downside to this. If https doesn’t work yet on one site or isn’t used by one plugin yet, the situation might change any time. Catching thumbnails with and without the protocol and/or www prefixes might also be unnecessary with some sites at this point in time, but might as well change later. Haven’t been able to think of a downside to the added support yet.

    Any other help you might need with the plugin I’m glad to give it a shot.

    The patch was further developed to support Dailymotion with https and with or without the protocol prefix and/or www. A major addition is useful error messages in case something goes wrong, which helps debugging problem cases a lot. The complete updated file can be viewed and downloaded through a link in my post in [Plugin: Video Thumbnails] Vimeo thumbnails not found.

    By the way the plugin works splendidly on WordPress 3.3, you might want to update that in the WP Plugin page.

    If File Exists is worth noting if we want to check for the existence of files locally.

    Plugin Author Sutherland Boswell


    Got the changes incorporated and made some additional changes that will hopefully fix the blank settings page bug some people were seeing. About to publish as 1.7.7, thanks again!

    PS – Just saw your 1.7.6-daedalon2 version a bit too late, those changes won’t be included in this version.

    I’ll try to find the changes of 1.7.6 to 1.7.7 and implement them on 1.7.6-daedalon2. Spent more than a full day on that one after -daedalon and it’d be a shame to not let the community benefit from the work. Let’s see if WinMerge is the right tool for the task.

    1.7.7-daedalon3 done, took only several hours with ExamDiff. Would have taken less than two without adding new patches. Changes since 1.7.6-daedalon2:

    Line 54: Changed dailymotion_info_retrieval -> bliptv_info_retrieval. (this error was actually left in both)

    Line 97: Changed $output = -> return and dailymotion_info_retrieval -> metacafe_info_retrieval. (the latter error was left in both, the first Sutherland had spotted and corrected already in 1.7.7)

    Line 134, 138, 143 and others: Changed . to \. in the domain parts of all URLs in all regexes. Now “” doesn’t match anymore. (a completely new change)

    Line 148: Added support for messy Youtube URLs by changing the regex from #(?:https?(?:a|vh?)?://)?(?:www\.)?\?v=([A-Za-z0-9\-_]+)#s to #(?:https?(?:a|vh?)?://)?(?:www\.)?youtube\.com/watch\?.*v=([A-Za-z0-9\-_]+)#. For some reason couldn’t get for example [A-Za-z0-9=&\-_]* to work instead of .* so now it’s a bit more loose than is optimal. Shouldn’t pose any problems though thanks to the next change. (a completely new change)

    Line 138, 143, 148 and others: Changed #s to # in the end of all regexes that matched URLs only or the WP Youtube Lyte tag. #s makes . match newlines, and using this unnecessarily can have negative implications. URLs don’t have newlines. Regexes matching <object> tags still match newlines with dots. (a completely new change)

    Lines 383 and 444: Changed width="100%" to style="max-width:100%;" to make admin widget not stretch the thumbnail bigger than it is. (a completely new change)

    Line 499: Changed cURL to be activated on your server.<BR><BR>Google should be able to help you. -> <a href="">libcurl</a> to be activated on your server. (a completely new change)

    After line 600 under Administration: Made the same change as 1.7.6 -> 1.7.7 by replacing the order of the two function declarations. Formatted according to the same standard as rest of the code. (this was the main change in 1.7.6 to 1.7.7 in addition to my patches, correct? or did I miss something?)

    Other lines: Removed commented out old code that was also removed in 1.7.7.

    Tested to get the thumbnails through scan past posts and individual reset functions for Youtube, Vimeo, Metacafe, Dailymotion and Bliptv.

    Possible improvements:

    Sutherland: Publishing a post doesn’t find a video thumbnail for me, I only get We didn't find a video thumbnail for this post.

    Should we use wp_die() and give a message instead of die() without a message on row 450?

    Plugin Author Sutherland Boswell


    TextWrangler does a pretty good job of comparing differences, I went through and included all the changes as well as converting all indentation to tabs.

    Here’s where I’m at: 1.7.8-working

    So you aren’t getting anything when the post is published and have to use the “search again” button every time?

    According to the documentation on AJAX in plugins, die() is required at the end of a callback function.

    Thanks for incorporating these to 1.7.8-working, looking forward to it. One issue we could look into before releasing it: publishing or updating a post that is set to private does not search for a thumbnail automatically. Didn’t yet find what could correct this. Any ideas?

    So you aren’t getting anything when the post is published and have to use the “search again” button every time?

    If you mean the messy Youtube URLs, here’s a way to clarify of the problem in repeatable steps:

    1. Enable Automatic embeds in /wp-admin/options-media.php.
    2. Watch a Youtube video until the end (or straight forward to the end) and click one of the related videos displayed over the video.
    3. Copy-paste the video URL from browser’s address bar to a WordPress post and press Publish. The link format will be
    4. When you view the post, you see the video embedded in the post. However a video thumbnail is not found automatically and will not be found by pressing Search Again.

    The newest patch corrects the problem by adding the loose-matching .* so anything in between watch? and v= is omitted. The abovementioned “messy” Youtube URLs work and so do all those that did before the patch. Now all auto-embeds supported by WP core get thumbnailed correctly.

    This intentionally doesn’t yet support Youtube URLs from channels such as WP core doesn’t support them so creating thumbnails from such links might be unwanted.

    Plugin Author Sutherland Boswell


    Can you try adding add_action('new_to_private', 'save_video_thumbnail', 10, 1); to the section below and let me know if that fixes the problem for private posts?

    add_action('new_to_publish', 'save_video_thumbnail', 10, 1);
    add_action('draft_to_publish', 'save_video_thumbnail', 10, 1);
    add_action('pending_to_publish', 'save_video_thumbnail', 10, 1);
    add_action('future_to_publish', 'save_video_thumbnail', 10, 1);

    Early on I was having trouble getting the action to run without creating duplicates for every revision or auto-save, and adding actions for these post status transitions was the best solution I could come up with at the time. I’m gonna be working on version 2.0 soon (your help has provided a good motivation) with several optimizations and this is probably one of the things I’ll try to improve.

    That one unfortunately doesn’t work. According to documentation such hook doesn’t exist, and testing confirmed this. The closest thing found was private_to_publish. Adding that solved a portion of the problem, albeit a tiny one: if the private is changed to be published, then the thumbnail is generated automatically.

    Plugin Author Sutherland Boswell


    That’s odd, it’s supposed to exist

    It might be time to reopen that ticket in that case. No documentation has that hook, it didn’t work in the previous test (creating new post, setting the visibility to private and clicking publish), and appropriately searching for wordpress "new_to_private" yields no trail of anyone using it or recommending it – apart from this thread 😉

    According to linked in the page you linked to, they were dynamically built at least back 4 years ago:

    function wp_transition_post_status($new_status, $old_status, $post) {
    	if ( $new_status != $old_status ) {
    		do_action('transition_post_status', $new_status, $old_status, $post);
    		do_action("${old_status}_to_$new_status", $post);
    	do_action("${new_status}_$post->post_type", $post->ID, $post);
    Plugin Author Sutherland Boswell


    Did a little testing myself and apparently the post’s status must be getting changed to draft before being privately published. I managed to get draft_to_private to work after creating a new post, switching it to private, and then clicking “Update”.

    I think the solution may be to just simplify things and only use the save_post hook.

    I tested this out and it seems to work fine, can do a little testing to see if it works for you without creating duplicate images?

    // Find video thumbnail when saving a post, but not on autosave or revisions
    add_action( 'save_post', 'save_video_thumbnail', 10, 1 );
    function save_video_thumbnail( $post_id ) {
    	if ( !wp_is_post_revision( $post_id ) ) {
    		$post_type = get_post_type( $post_id );
    		$video_thumbnails_post_types = get_option( 'video_thumbnails_post_types' );
    		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    			return null;
    		} else {
    			// Check that Video Thumbnails are enabled for current post type
    			if ( in_array( $post_type, (array) $video_thumbnails_post_types ) || $post_type == $video_thumbnails_post_types ) {
    				get_video_thumbnail( $post->ID );
    			} else {
    				return null;

    Thanks again for your help!

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘[Plugin: Video Thumbnails] Patch: Support https(a|vh),, private, and mass recreate deleted’ is closed to new replies.