Php tips, wordpress hacks

Add an mp3 player in your blog post

In this tutorial I will show you a simple way to add an mp3 player in your blog posts using shortcodes.
For the start I chose an open source mp3 player that can be downloaded from here. The swf file, from this player, must be uploaded in the same directory with your default wordpress theme (Ex. /wp-content/theme-name/).
Add this piece of code in your functions.php file:

function show_player($atts) {
 extract(shortcode_atts(array(
 'path' => '',
 'width' => '200',
 'height' => '20'
 ), $atts));
 return '<object type="application/x-shockwave-flash" data="wp-content/themes/theme-name/player_mp3.swf" width="'.$width.'" height="'.$height.'">
 <param name="movie"; value="player_mp3.swf">
 <param name="FlashVars" value="mp3='.$path.'">
 </object>';
 }

add_shortcode('mp3', 'show_player');

Next, if you want to add the player in your blog post you can simply add this shortcode anywhere in your page:

[mp3 path="wp-content/uploads/song.mp3"]

Or if you want to customize the size of your player you can add:

[mp3 path="wp-content/uploads/song.mp3" width="100" height="10"]

It’ s that

simple!

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>