Display PowerPress in your WordPress themes

(version française ici)

Even if PowerPress,  the excellent WordPress podcast manager, offers the_powerpress_content() to insert the media player in the theme, this function only works inside the WordPress Loop.

¿Que pasa?

Behind the curtain, this function looks for podcasts information generated by PowerPress inside the article on which the Loop is currently stopped. If you put this function in the header, the Loop is set on the first article, so the media player will not show up if the first article doesn't contain a podcast.

The fix

The get_player() function below will find the first article in the Loop that contains the custom keys used by PowerPress. To use it, simply paste the following code in your functions.php file.

function create_player(){
	//Use this function to insert the Powerpress player anywhere in the page.
	//Made by Nicolas Bouliane (http://nicolasbouliane.com/)

	/*We're going to use the Loop to retrieve the latest post with the 'enclosure' custom key set
	//then interpret it and manually launch powerpressplayer_build with the URL contained within
	//that data.*/

	//Let's reset the Loop to make sure we look through all posts

	//Loop through each post
	while (have_posts()) : the_post();
		//Check if there are custom keys for this post
		if( get_post_custom($post->ID)){
			//Check the record for the 'enclosure' key
			$meta = get_post_custom($post->ID);
				//Split it as to retrieve what matters most: the file URL which should be on the
				//first line (I didn't test much)
				$url = split("n",$meta['enclosure'][0],2);
				$url = $url[0];
				//Create a player and end the loop. That was easy!
				$Settings = get_option('powerpress_general');
				echo(powerpressplayer_build( $url, $Settings, $ExtraData ));

	/*Reset the loop again in case this code ends up in the header. Otherwise, the posts would
	//start from the first podcast that was found.*/

Leave a comment