Error when overriding only some audio shortcode HTML output

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 Answer
1

This should work, regardless how many 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.

Leave a Comment