Wordpress and Webdesign Forum go41 » WordPress Code Snippets

  1. As explained in a previous topic get the oembed code of a video in a WP 2.9 post from postmeta we can get only the code for embeded media (WordPress 2.9 required!)

    To change and resize the width and height of the video we are going to display, we can add a function to the theme's function.php to do this for us.
    I got this function from Vinod Kumar explained here:
    Dynamically Change Width and Height in embed code

    So put this function into your functions.php first:

    // the function to resize oembed videos
    function resizeMarkup($markup, $dimensions)
    {
    $w = $dimensions['width'];
    $h = $dimensions['height'];
    
    $patterns = array();
    $replacements = array();
    if( !empty($w) )
    {
    $patterns[] = '/width="([0-9]+)"/';
    $patterns[] = '/width:([0-9]+)/';
    
    $replacements[] = 'width="'.$w.'"';
    $replacements[] = 'width:'.$w;
    }
    
    if( !empty($h) )
    {
    $patterns[] = '/height="([0-9]+)"/';
    $patterns[] = '/height:([0-9]+)/';
    
    $replacements[] = 'height="'.$h.'"';
    $replacements[] = 'height:'.$h;
    }
    
    return preg_replace($patterns, $replacements, $markup);
    }

    Having this code activated by putting it in functions.php, we can use it as explained in Vinod's post (link above).

    We want to use this function to change width and height attributes throughout the oembed code we retrieved before from the WordPress database.
    As explained here: http://forum.go41.de/topic/get-the-oembed-code-of-a-video-in-a-wp-29-post-from-postmeta we have already a variable assigned to the oembed code, now we just pass this variable to the function 'resizeMarkup' with new values for width and height and echo the resized video clip.
    To do this we put into the loop this code:

    <?php if(function_exists('resizeMarkup')) {
    	$resizedvideo = resizeMarkup(($embedhtml), array(
    	'width'=>300,
    	'height'=>250 ));
    	echo $resizedvideo; } ?>

    Any questions? Sign up and ask, you are welcome.

    Posted 8 months ago #

RSS feed for this topic

Reply

You must log in to post.

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