I'm testing the new Wordpress 3.6 Release Candidate 2 and it's [audio] shortcode and there is something I can't figure out. The post contains the following content:

[audio http://wpthemetestdata.files.wordpress.com/2008/06/originaldixielandjazzbandwithalbernard-stlouisblues.mp3]

How can I extract this shortcode and it's URL and display it as a player in the post preview in index.php? I found the new functions at the bottom of this page ( http://codex.wordpress.org/Version_3.6 ) but I can't figure it out how can I manually display the player on the index as it is shown in single.php.


You missed the src part. It should have been:

[audio src="http://wpthemetestdata.files.wordpress.com/2008/06/originaldixielandjazzbandwithalbernard-stlouisblues.mp3"]

Go on the Shortcode section of the Audio / Video support in Core post.

$post_content = 'lorem ipsum dolor [audio src="http://wpthemetestdata.files.wordpress.com/2008/06/originaldixielandjazzbandwithalbernard-stlouisblues.mp3"] lorem [audio src="http://wpthemetestdata.files.wordpress.com/2008/06/originaldixielandjazzbandwithalbernard.mp3"] ipsum ';

if( preg_match( '#\[audio\s*.*?\]#s', $post_content, $matches ) && preg_match('/"([^"]+)"/', $matches[0], $m) ){
   echo $m[1]; // echoes http://wpthemetestdata.files.wordpress.com/2008/06/originaldixielandjazzbandwithalbernard-stlouisblues.mp3

  //set as featured 


The first regex will get your first [audio] shortcode and the second regex will extract the URL from it by looking what's in between the double quotes.

Note that there is get_shortcode_regex which should also help you.

| improve this answer | |
  • The shortcode is working correctly. The problem is how can I extract the URL from the shortcode and insert it in something like Featured Image, but let's say it is Featured Audio. In other words I want a player in the post preview section that plays the file, which is in the content of the post. – Ziik Jul 31 '13 at 15:48
  • Updated answer. – RRikesh Aug 1 '13 at 5:11
  • Great! That exactly what I need. – Ziik Aug 1 '13 at 7:49
  • Only one question if I may. I'm using wp_audio_shortcode function and I'm passing array with src="url to the mp3 file" and it does nothing. Is this the correct function and do you have any idea why it's not working? :\ @rrikesh – Ziik Aug 1 '13 at 11:09
  • Is that your question? Try echo wp_audio_shortcode( $attr ); – RRikesh Aug 1 '13 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.