WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get First Embed or Object (6 posts)

  1. freshifreshy
    Member
    Posted 3 years ago #

    Does anyone know a way that I could check a post for an embedded video and then do something with that video?

    I want to create a video thumbnail (or a small version of the video) for posts that contain embedded videos, but I'm having a hard time figuring out how I might do this...

  2. esmi
    Theme Diva & Forum Moderator
    Posted 3 years ago #

  3. freshifreshy
    Member
    Posted 3 years ago #

    If it's embedded, it's not going to show up as an attachment... Not sure how I could use this...

  4. esmi
    Theme Diva & Forum Moderator
    Posted 3 years ago #

    Sorry - I was thinking of a video file that you'd uploaded. I don't think there's an easy way to achieve what you're after. Essentially, you would need to search the whole post content for a specific shortcode or url.

  5. freshifreshy
    Member
    Posted 3 years ago #

    I ended up writing a PHP function. It looks like this.

    function show_thumb() {
    
    	global $post, $posts;
    
    	$thumb = '';
    
      	ob_start();
      	ob_end_clean();
    
    	$output = preg_match_all('/(\<object.*\<\/object\>)/is', $post->post_content, $matches);
    	$thumb = $matches [1] [0];
    
    	if(empty($thumb)) {
    
    		$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
      		$thumb = $matches [1] [0];	
    
    		if(empty($thumb)) {
    
    			$thumb = "/images/default.jpg";
    
    		} else {
    
    			return '<img src="'.$thumb.'" alt="thumb-'.$post->ID.'" />';
    
    		}
    
    	} else {
    
    		return $thumb;
    
      	}
    
    }
  6. Tara Rotten
    Member
    Posted 3 years ago #

    Hey,

    Your code is great, cant get it to pull in the object's ie videos though.

    Ive wrapped all videos in a div (<div class=video>) is there a way to search for this div instead. (all videos are embeded from youtube)

Topic Closed

This topic has been closed to new replies.

About this Topic