Avis aux francophones / Message to the french speaking blokes: Ce billet représente un complément de réponse à un article paru sur un blog anglophone (la version française est en chantier). Ce code a été transformé en plugin WordPress! Cliquez ici pour le télécharger!

 
YouTube on WordPress08/09/2011 update – This code has been updated as a WordPress plugin! Click here to download.

Let me explain: I’ve been searching for a quick way to have a better grip on the YouTube shortcode that comes by default in the WordPress package. After a while it’s clear that it would be easier to make another one instead and I found a piece of code doing just that on the WordPress Code Snippets blog. I began to look at it, tweak it a little then decide to rewrite it in order to improve it and make it bullet proof.

The result is a function with 5 parameters (not counting the ID of the video itself): the width & the height of the player, the full screen access, the display of the buttons and the autohide feature. It always allows script access (for those who need it) and forces the use of Action Script 3 engine (just to be sure everybody have the same options). If you wish to check all the available parameters of the YouTube player for eventual modifications or else go to this url:

  • http://code.google.com/intl/fr/apis/youtube/player_parameters.html
    •  
      So this is the code to cut & paste in the functions.php of your active theme:

       

      <?php 
      
      function pixel_youtube($atts)	{
      	// WP-Pixel-YouTube v1.0 http://pxsol.info/p0wOuK
      
      	//configuration
      	extract(shortcode_atts(array(
      		'the_id' =>			'',			// the ID of the YouTube video * SHOULD STAY EMPTY *
      		'width' =>				'640',		// the width for the player
      		'height' =>				'390',		// the height for the player
      		'allowFullScreen' =>	'true',		// offer or not full screen access
      		'controls' =>			'true',		// display or not the control buttons
      		'autohide' =>			'true',		// hide or not the controls after video start
      	), $atts));
      
      	//URL creation
      	$optionsSwitch = '';
      	if ($allowFullScreen = 'true') {
      		$optionsSwitch = $optionsSwitch.'&fs=1';
      	} else {
      		$optionsSwitch = $optionsSwitch.'&fs=0';
      	};
      	if ($controls = 'true') {
      		$optionsSwitch = $optionsSwitch.'&controls=1';
      	} else {
      		$optionsSwitch = $optionsSwitch.'&controls=0';
      	};
      	if ($autohide = 'true') {
      		$optionsSwitch = $optionsSwitch.'&autohide=1';
      	} else {
      		$optionsSwitch = $optionsSwitch.'&autohide=0';
      	};
      
      	//generation
      	$generated_output = '<object style="height: '.$height.'px; width: '.$width.'px">
      	<param name="movie" value="http://www.youtube.com/v/'.$the_id.'?version=3'.$optionsSwitch.'"></param>
      	<param name="allowFullScreen" value="'.$allowFullScreen.'"></param>
      	<param name="allowScriptAccess" value="always"></param>
      	<embed src="http://www.youtube.com/v/'.$the_id.'?version=3'.$optionsSwitch.'" type="application/x-shockwave-flash" 
      	allowfullscreen="'.$allowFullScreen.'" 
      	allowScriptAccess="always" 
      	width="'.$width.'" 
      	height="'.$height.'">
      	</embed></object>';
      
      	//error catcher
      	if (empty($the_id)) {
      		$generated_output = '<span class="error-pixel_youtube">You must set the ID of the YouTube video.</span>';
      	};
      
      	//output
      	return $generated_output;
      }
      add_shortcode('youtube_video', 'pixel_youtube');
      
      ?>
      
      

       

      And to use it, just put the shortcode below in your text and replace the value of « the_id » by… well, the ID of the YouTube video you wish to see (unless you like kittens very much)! 😉

       

      You must set a valid YouTube ID, please try again.
      

       

      This script is free to use and will be upload to GitHub soon if anybody wants to improve it in any way (for exemple I’d like to see the HTML5 part added). If you wish to distribute this snippet consider not to remove the first comment in the source code PLEASE. It has been created and tested on a WordPress 3.2.1 installation.

       
      For history: a screenshot of the webpage containing the initial idea for this script

      Original script screenshot

      © wpsnipp.com

      Commenter