WordPress.org

Ready to get started?Download WordPress

Forums

Auto Post Thumbnail
auto-post-thumbnail hack to use the youtube image when no other images (3 posts)

  1. _ck_
    Member
    Posted 2 years ago #

    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.

  2. skittles
    Member
    Posted 2 years ago #

    Cant believe this isnt in by default already!

  3. Gala123
    Member
    Posted 2 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic