Support » Plugin: Auto Featured Image (Auto Post Thumbnail) » auto-post-thumbnail hack to use the youtube image when no other images

  • This is a quick and dirty hack to force the auto-post-thumbnail to use the first available youtube image for a thumbnail if no other image is available in the post and it contains almost any kind of youtube embed.

    Find

    // Get all images from post's body
        preg_match_all('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'>]*)/i', $post[0]->post_content, $matches);

    and add immediately afterwards

    if (empty($matches[0])) {
    		preg_match('%(?:youtube\.com/(?:user/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $post[0]->post_content, $match);
    		if (!empty($match[1])) {
    			$matches=array(); $matches[0]=$matches[1]=array('http://i3.ytimg.com/vi/'.$match[1].'/hqdefault.jpg');
    		}
    	}

    Not well tested but seems to work.

Viewing 2 replies - 1 through 2 (of 2 total)
  • skittles

    (@skittles)

    Cant believe this isnt in by default already!

    Does this also work for other embedded videos? Vimeo? etc. or should the code of the match be altered for that? I think it is this code right:

    preg_match(‘%(?:youtube\.com/(?:user/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^”&?/ ]{11})%i’, $post[0]->post_content, $match);

    Should become
    preg_match(‘%(?:vimeo\.com/(etc. etc.

    What should I do to make it like that? (PHP skill basic, but ready to learn! 🙂

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘auto-post-thumbnail hack to use the youtube image when no other images’ is closed to new replies.