2

I have recently redone a site that was originally created in 2011. Of course there were a few big development issues and one of them is the usage of an old audio shortcode:

[audio:http://localhost:8888/lusa/audio/1310seg02.mp3]

I have not seen any documentation of the native Wordpress audio shortcode ever using a colon. I have also not been able to find a plugin that uses it in this way.

Does anyone know how I can get this shortcode functioning? I believe my options are.

  1. Create a script that turns this audio shortcode into the newer format [audio src="http://localhost:8888/lusa/audio/1310seg02.mp3] https://codex.wordpress.org/Audio_Shortcode.
  2. Find the plugin that originally made the shortcode this way.
  3. ?

/======== Progress ========/

@gmazzap got me on the right track! The issue is that with the shortcode_atts_audio hook the $atts variable doesn't output strings outside of the predefined attributes (src, loop, autoplay, preload.) After some digging I found that I can use the wp_audio_shortcode_override to access my url. That is what I'm doing in the code below. But now I'm having trouble passing that attribute back into the shortcode and outputting it.

function legacy_audio_shortcode_converter( $html, $attr ) {

        $colon_src = $attr[0]; //get the url string with the colon included.
        $attr['src'] = substr($colon_src, 1); //filter out the colon
        $new_audio_src = $attr['src']; //save the url as the official audio src
        var_dump($new_audio_src); //this is currently outputing the exact url I need but not sure how to make sure the player shows up with this new src.
}

add_filter( 'wp_audio_shortcode_override', 'legacy_audio_shortcode_converter', 10, 2 ); 

2 Answers 2

1

If your only issue is that the format is incorrect then switch it in the_content hook with the correct version.


Non-local content:

[audio:http://www.soundhelix.com/examples/mp3/SoundHelix-Song-7.mp3]

Place in a plugin or functions.php

// hook earlier than 10

add_filter('the_content', 'wpse_20160110_the_content_fix_audio', 0);

function wpse_20160110_the_content_fix_audio($content){
    return str_replace ( '[audio:', '[audio src=', $content );
}
1
  • awesome. that worked great. Simple.
    – Celso
    Commented Jan 11, 2016 at 2:52
1

I think you can use 'shortcode_atts_audio' filter hook to convert the arguments in the proper form, and let the default audio shortcode handler render it.

In fact, with a shortcode like [audio:http://localhost:8888/lusa/audio/1310seg02.mp3] the shortcode handler will be called with an argument array that is

array(0 => ':http://localhost:8888/lusa/audio/1310seg02.mp3');

Where the proper formatted argument array should be

array('src' => 'http://localhost:8888/lusa/audio/1310seg02.mp3');

So you can do:

add_filter( 'shortcode_atts_audio', function(array $atts) {
  if (
    empty($atts['src'])
    && ! empty($atts[0])
    && filter_var(ltrim($atts[0], ':'), FILTER_VALIDATE_URL)
  ) {
      $atts['src'] = ltrim($atts[0], ':');
  }

  return $atts;
} );

In this way the default audio shortcode handler should be able to render the shortcode.

Untested.

3
  • Thanks @gmazzap! I didn't even consider filters. There were a few issues with the code. One being that "if (empty($atts['src']))" worked and "if (isset($atts['src']))" did not. I also found it much easier to grab my url with the colon using the wp_audio_shortcode_override hook. Unfortunately having a bit of trouble still. Can you take a look at my edited answer/code and let me know if you have any thoughts? Thanks!
    – Celso
    Commented Jan 10, 2016 at 21:30
  • @Celso I fixed the code using empty instead of isset and simplified it. Now should work and be easy to understand... Why do you think that to use wp_audio_shortcode_override is easier? I think it is not, in fact you have problem using it ;) Reason is that from wp_audio_shortcode_override you can't just change arguments, but you need to render the shortcode. Moreover, my code works for both your legacy shortcode and for proper formatted shortcodes, this gives you chance to use the proper formatted shortcode from now.
    – gmazzap
    Commented Jan 10, 2016 at 21:50
  • thanks for simplifying it. The issue is that $atts variable doesn't output the string I need. It only outputs the default attributes (src, loop, autoplay, preload.) I tested just doing a var_dump($atts) and var_dump($atts[0]) using your code and could not see the string I needed. If I could get that string then this would work but unfortunately I can't.
    – Celso
    Commented Jan 11, 2016 at 2:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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