WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an audio player than uses the following syntax to display its player:

[audio src="http://somedomain.com/wp-content/uploads/2013/01/songtitle.mp3"]

The thing is, I'm pretty sure I can show the user how to copy the url and paste it in the post, but how can I search the content for a .mp3 link and then put [audio src=" in front of it and "] at the end of it?

share|improve this question
up vote 6 down vote accepted

Filter the_content ond/or the_excerpt and replace audio URLs that are not an attribute value already.


add_filter( 'the_content', 'wpse_82336_audio_url_to_shortcode', 1 );
add_filter( 'the_excerpt', 'wpse_82336_audio_url_to_shortcode', 1 );

function wpse_82336_audio_url_to_shortcode( $content )
    # See http://en.wikipedia.org/wiki/Audio_file_format
    # Adjust the list to your needs
    $suffixes = array (
        '3gp', 'aa3', 'aac', 'aiff', 'ape', 'at3', 'au',  'flac', 'm4a', 'm4b',
        'm4p', 'm4r', 'm4v', 'mpc',  'mp3', 'mp4', 'mpp', 'oga',  'ogg', 'oma',
        'pcm', 'tta', 'wav', 'wma',  'wv',

    $formats = join( '|', $suffixes );
    $regex   = '~
    (([^"\'])|^)            # start of string or attribute delimiter -> match 1
    (https?                 # http or https
        ://                 # separator
        .+/                 # domain plus /
        .+                  # file name at least one character
        \.                  # a dot
        (' . $formats . ')  # file suffixes
    )                       # complete URL -> match 3
    (([^"\'])|$)?           # end of string or attribute delimiter -> match 5
    ~imUx';                 # case insensitive, multi-line, ungreedy, commented

    return preg_replace( $regex, '\1[audio src="\3"]\5', $content );
share|improve this answer
This is awesome!! You are the man, this is what I was trying to do, just wasn't sure how to go about it - or even where to start! – Josh Rodgers Jan 20 '13 at 4:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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