Wordpress and Webdesign Forum go41 » WordPress Code Snippets

  1. The latest WordPress 2.9 got a new function to automatically embed all plain text URLs with the help of oembed. On a blog displaying resized images together with the_excerpt it is nice to show also the embeded video clips if there is no image.
    So how to get the code to embed a video from the database?
    In WordPress 2.9, if you insert a plain URL link to a video from an oembed enabled site, WordPress will try to put the whole code around this URL to embed this video in a player.
    This code will not show up in edit posts, but is written to postmeta table. The meta_key is looking like '_oembed_74dd854eb13572c16.....' and it's meta_value is the actually html code to get parsed.

    Working inside the loop, we are going to get the unique meta_key of a post starting with oembed. If in a post is this meta_key we assign this to a variable (here: $oembkey)
    Next we get the value of this key, which is actually the full html embed code (here: $embedhtml)

    Finally we can echo $embedhtml to display only the video without the_content or the_excerpt.

    The code to get only the embeded video:

    <?php
      $custom_field_keys = get_post_custom_keys();
      foreach ( $custom_field_keys as $key => $value ) {
        $valuet = trim(substr($value, 1, 4));
    	if ( 'oemb' == $valuet ) { // look if a oembed key exists
        $oembkey = $value; //now set the the key we found as oembkey
    	$oembvalue = get_post_custom_values($oembkey); //get the value of oembkey as oembvalue
    	if (isset($oembvalue[0])) {
    		$embedhtml = ($oembvalue[0]);
    	  }
    	}
      }
    ?>

    and now echo it:

    <?php echo $embedhtml; ?>

    In another topic I will explain how to resize this player here:
    Change width and height in oembed code on the fly WP 2.9

    Posted 2 years ago #

RSS feed for this topic

Reply

You must log in to post.

Join us! or log in (lost password?):