[Plugin: Video Thumbnails] FIX for use with Automatic YouTube Posts plugin
-
Okay, after doing a ton of work, I think I figured out why this plugin has some issues working with the Automatic YouTube Video Posts plugin. Here’s what I came up with by looking at the plugin code…..
// Get the post or custom field to search if ( $video_key = get_option( 'video_thumbnails_custom_field' ) ) { $markup = get_post_meta( $post_id, $video_key, true ); } else { $post_array = get_post( $post_id ); $markup = $post_array->post_content; $markup = apply_filters( 'the_content', $markup ); }In the above section, the plugin checks first to see if the user has added the option of Custom Field in the admin section. If you’re using the AYVP Plugin, make sure you have that field set to ‘_tern_wp_youtube_video’. If you have that set, this plugin will see that it needs to check that custom meta field to find the YouTube video ID and will save that ID in the $markup variable. Otherwise, it scans the post itself for an embedded video and saves the content of the post (after being filtered) into the $markup variable.
// Checks for the old standard YouTube embed preg_match( '#<object[^>]+>.+?https?://www\.youtube(?:\-nocookie)?\.com/[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?://www\.youtube(?:\-nocookie)?\.com/[ve]/([A-Za-z0-9\-_]+)#', $markup, $matches ); } // Checks for YouTube iframe, the new standard since at least 2011 if ( !isset( $matches[1] ) ) { preg_match( '#https?://www\.youtube(?:\-nocookie)?\.com/embed/([A-Za-z0-9\-_]+)#', $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\.)?youtube(?:\-nocookie)?\.com/watch\?.*v=([A-Za-z0-9\-_]+)#', $markup, $matches ); } // Checks for any shortened youtu.be 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?)?://)?youtu\.be/([A-Za-z0-9\-_]+)#', $markup, $matches ); } // Checks for YouTube Lyte if ( !isset( $matches[1] ) && function_exists( 'lyte_parse' ) ) { preg_match( '#<div class="lyte" id="([A-Za-z0-9\-_]+)"#', $markup, $matches ); }The next part of the plugin does 2 things. It either checks the $markup variable for a match of a particular string to determine whether or not there is an embedded video in the post content. This uses the function preg_match(). If there IS a match, (the match is actually the ID of the YouTube video), it’s saved in the $match[1] variable.
But if your video ID is saved in a custom field, there’s no need to run the preg_match() function on the post content because you already have the ID in the $markup. The next bit of code shows where there is a mistake…..
// If we've found a YouTube video ID, create the thumbnail URL if ( isset( $matches[1] ) ) { $youtube_thumbnail = 'http://img.youtube.com/vi/' . $matches[1] . '/0.jpg'; // Check to make sure it's an actual thumbnail if ( !function_exists( 'curl_init' ) ) { $new_thumbnail = $youtube_thumbnail; } else { $ch = curl_init( $youtube_thumbnail ); curl_setopt( $ch, CURLOPT_NOBODY, true ); curl_exec( $ch ); $retcode = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); // $retcode > 400 -> not found, $retcode = 200, found. curl_close( $ch ); if ( $retcode == 200 ) { $new_thumbnail = $youtube_thumbnail; } } }The important thing to notice is that it’s checking the $matches[1] variable for the video ID. But that variable won’t be set if the video ID is stored in a custom field instead of a traditional embedding method. That’s only set if there IS a match in the preg_match() function that’s run on the content of the post.
The only section that needs changed is the one above. Replace this….
// If we've found a YouTube video ID, create the thumbnail URL if ( isset( $matches[1] ) ) { $youtube_thumbnail = 'http://img.youtube.com/vi/' . $matches[1] . '/0.jpg';…with this…..
// If we've found a YouTube video ID, create the thumbnail URL if ( isset( $video_key ) ) { $youtube_thumbnail = 'http://img.youtube.com/vi/' . $markup . '/0.jpg';What this does is check to see if the $video_key variable is set. In our case, $video_key IS set, which means that the $markup variable contains ONLY the YouTube video ID. So the address of the YouTube video thumbnail needs the $markup variable rather than the $matches[1] variable, which would be empty if $video_key is set.
Anyway, that’s the way I fixed it. I’d recommend that the plugin author checks it out and edits the plugin code accordingly…especially since it seems to be a common issue that users have.
Hope that helps you guys!
The topic ‘[Plugin: Video Thumbnails] FIX for use with Automatic YouTube Posts plugin’ is closed to new replies.