WordPress.org

Ready to get started?Download WordPress

Forums

Video Thumbnails
[resolved] FIX for use with Automatic YouTube Posts plugin (7 posts)

  1. wspencer
    Member
    Posted 2 years ago #

    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!

    http://wordpress.org/extend/plugins/video-thumbnails/

  2. Sutherland Boswell
    Member
    Plugin Author

    Posted 2 years ago #

    I've suggested similar fixes for plugins that only store the video ID in a custom field, but the problem is knowing it's only the ID and what service it belongs to. It looks like I could add if (function_exists('WP_ayvpp_init')) and then run the code specific to AYVP.

  3. wspencer
    Member
    Posted 2 years ago #

    Yes, I think that would work as well and is probably the best solution. In fact, I would add specifics to that if statement that the ayvpp stores the ID of the video in a custom field...so you could just go ahead and do....

    if (function_exists('WP_ayvpp_init')) {
        $video_key = '_tern_wp_youtube_video';
        $markup = get_post_meta( $post_id, $video_key, true);
        $youtube_thumbnail = 'http://img.youtube.com/vi/' . $markup . '/0.jpg';
       }
    else {
        /* check the post content for embed codes and run the preg_match() functions */

    Or you could just add another case to the preg_match() function that checks to see exactly WHAT is stored in the custom field. If it's only an ID, then it would work like the code I posted above. Or maybe just do something like.....

    // If we've found a YouTube video ID, create the thumbnail URL
    		if ( !isset( $matches[1] ) && isset( $video_key ) {
    			$youtube_thumbnail = 'http://img.youtube.com/vi/' . $markup . '/0.jpg';

    If that stays in the same spot, if there were no matches at all in the preg_match() function AND there is a $video_key set in the admin section, then it can be assumed that the custom field contains only the ID of the video.

    Just a few ideas. Hopefully that helps you out! It seems like a lot of people use your plugin along with the AYVP plugins.....myself included.

    If there's anything I can help with, please let me know!

  4. Sutherland Boswell
    Member
    Plugin Author

    Posted 2 years ago #

    I'll be making a post about version 2.0 soon and I'm looking for some testers. You can sign up here.

  5. thebitgen
    Member
    Posted 2 years ago #

    Hey guys,

    I've tried all of this and then some and Video Thumbnail is still returning a "Couldn't find a video thumbnail for this post" error.

    I've messed around with the "tern_wp_youtube_video" call to see if I can get it to change error messages and of course it does. I'm not sure what I can be doing wrong though.

    Thoughts?

  6. Sutherland Boswell
    Member
    Plugin Author

    Posted 2 years ago #

    Right after this section:

    // Simple Video Embedder Compatibility
    if ( function_exists( 'p75HasVideo' ) ) {
    	if ( p75HasVideo( $post_id ) ) {
    		$markup = p75GetVideo( $post_id );
    	}
    }

    try adding:

    // AYVPP Mod
    if (function_exists('WP_ayvpp_init')) {
    	$video_key = '_tern_wp_youtube_video';
    	$markup = 'http://www.youtube.com/watch?v=' . get_post_meta( $post_id, $video_key, true);
    }
  7. thebitgen
    Member
    Posted 2 years ago #

    Unfortunately nothing.

    For troubleshootings sake- "Custom Field" in this case would be left blank correct? And selecting "Posts" as the post type will also not effect this?
    Could there be any conflicting plugins? If so what kind of plugin could interfere. In theory as long as the theme in use has featured images enabled, it should work?

    I tend to keep the plugin count very low. I try to keep things simple. Your help is greatly appreciated :D

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic