2

Context

I have a function that overrides audio shortcode HTML rendering, thanks to a WordPress hook:

wp_audio_shortcode_override

I used it this way in an OOP plugin:

add_filter( 'wp_audio_shortcode_override' , array( $this, 'wp_audio_shortcode_override' ), 10, 2 );

In order to prevent all the HTML shortcodes from being overridden, I want to create a custom shortcode attribute:

player="default"

As far I can read from the docs and the source code, if I return an empty string '', the override should be off. So I wrote this at the beginning of my render function:

public function wp_audio_shortcode_override( $html, $attr ) {

    $html = ''; // This is the value to disable the shortcode override

    if ( isset( $attr['player'] ) ) {
        if( $attr['player'] === 'default' )
            var_dump( $html );
            return $html;
    } 
    // After that, some code to fill the $html with thing to render,
    //   according to the shortcode attributes values

The next part of the function is how the shortcode must be overridden.

Results

  • Shortcode override without conditions works
  • Adding the condition in the function, and write the corresponding shortcode attribute player="default" in one audio shortcode, works.
  • Adding the same shortcode attribute value to all audio shortcodes in a post works.
  • Having audio shortcodes with the attribute and some without on the same post doesn't work.

Reproduce

For this, I started from a plugin example posted here: Is it possible to control the width of the Wordpress audio player?

Assistance

What did I miss? Why can I disable the override on all shortcodes, but not selectively on the same post?

1

This should work, regardless how many [audio] tags you have in the post, and regardless which have whatever attributes. My guesses are either the logic in your handler is slightly off, or a 3rd party (i.e. plugin) is hijacking the default shortcode handler behaviour.

To test, remove your code and try the following - it should clearly demonstrate if (or if not) the default shortcode & filter is working as expected:

function wpse_209708_debug_audio_shortcode( $html, $attr ) {
    if ( ! empty( $attr['player'] ) && $attr['player'] === 'default' )
        $html = '{audio player:default}';
    else
        $html = '{audio player:other}';

    return "<p>$html</p>";
}

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

Report back and I will update this with a "real" answer.

  • Thanks for your support ! - I deleted my filter, - I placed yours - On a post, three audio shortcode, onewith player="default" It appears as expected (two "others" one "default"). More, if I set $html ='', it works as expected (it renders "other" for the other, and the actual default player for the "default". So there must be something wrong in my code but I can't find what. I uploaded my plugin on github if you want to take a close look. I'll credit your for the help :) – X-Raym Nov 25 '15 at 16:03
  • My plugin just get accepted on official WordPress Repo as WaveSurfer-WP, it will be easier to install it and test it. I look forward to hearing from you. Cheers, and good luck ! – X-Raym Nov 26 '15 at 16:59

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.