How to filter atts for every shortcode in WordPress

I need to filter the $atts array for every shortcode (either every shortcode registered or just every shortcode in page) but can’t find a way which also caters for situations in which the attribute names may not be known beforehand.

Example: [shortcode known_attribute="value" 8c69f120="unknown attribute"]

Read More

I tried by looping through $GLOBALS['shortcode_tags'] and using the shortcode_atts_{$shortcode} filter but because shortcode_atts is a combination of array_merge and unset atts not in $pairs array, this won’t work.

Does anyone know how to apply a filter to $atts for every shortcode in a way which caters for shortcodes for which attribute names cannot necessarily be known beforehand?

Related posts

Leave a Reply

1 comment

  1. I’m not sure what you’re trying to do exactly, but you can catch any attribute and modify (or unset) the corresponding output with something like the following:

    foreach( array_keys( $GLOBALS['shortcode_tags'] ) as $tag ) {
        add_filter( "shortcode_atts_{$tag}", "so26932914_shortcode_atts", 25, 3 );
    }
    
    function so26932914_shortcode_atts( $out, $pairs, $atts )
    {
        if( isset( $atts['foo'] ) && '' != $atts['foo'] )
            $out['foo'] = 'bar';
    
        return $out;
    }
    

    One important caveat: shortcodes are not required to define the third $shortcode parameter, which is essential for the $shortcode_atts_{$shortcode} filter to do its thing.