Wrapping an existing shortcode with a new one

I am writing a wordpress plugin that extends an existing plugin’s behavior.

The existing plugin defines a shortcode named [original] and I define a shortcode named [wrapper] that adds some functionality, but basically behaves the same.

Read More

This is the code I wrote:

function wrapper_shortcode($atts,$content)
{
    //Do something...
    return do_shortcode("[original]$content[/original]");
}
add_shortcode( 'wrapper', 'wrapper_shortcode' );

My question is, how do I pass the attributes ($atts) next to the [original] shortcode, and is there a better way to do this than to call the do_shortcode() function ?

Related posts

1 comment

  1. If I am understanding correctly, you want to grab all shortcode parameters and blindly pass them from wrapper over to your original shortcode.

    I would not recommend passing parameters over without proper validation.

    You have a few options:

    1. As silver suggests, remove original and replace it with your own modified version (since you can’t update the plugin). Update it after each release of the plugin.

    2. Add the parameters you wish to validate and pass through. and post it back in the return.


    NOT RECOMMENDED – Just because you can; doesn’t mean you should.

    It does sound like you want to do something like the following – If you were to do this, you really could be asking for trouble, unless you validate each attribute as it comes through:

    function wrapper_shortcode($atts, $content) {
        $o_shortcode_atts = " ";
        foreach ($atts as $key_att => $val_att) {
    
            // Validate $key_att and $val_att here - FOR SECURITY!!!
    
            $o_shortcode_atts = $o_shortcode_atts . " ".$key_att."="".$val_att."" ";
        }
        trim($o_shortcode_atts," ");
    
        // Do something...
    
        return do_shortcode("[original".$o_shortcode_atts."]".$content."[/original]");
    }
    

    WARNING – THIS COULD BE ASKING FOR TROUBLE IF YOU DO NOT VALIDATE

Comments are closed.