Hijack WordPress Shortcode attribute

You have several options, but I would advise against what you’re doing, what you really need is to make a new shortcode that calls the plugins functions to achieve what you want ( or to use a plugin that provides a filter ):

1. Search Replace on the Post Content

Hook into the_content filter, and do a string search replace on the content before it’s processed by do_shortcodes. Note that you’ll need to check the post has that shortcode, find where the shortcode is, parse out the values, and swap them.

This is the harder solution, and will have a performance cost

2. Contextual Shortcode Wrappers

You could build a shortcode that wraps around the other shortcode, and then does solution 1) but only on the shortcodes inside. This relies on the shortcodes second parameter which contains the content inside the shortcode. This would be more useful than solution 1, while still requiring the same work.

It would be slightly faster too, but only because it doesn’t have to process the entire post content, so the savings are minimal.

3. Unregister The Shortcode and Re-register a Wrapper With The Same Name

Shortcodes map on to functions, so unregister the shortcode, create a new shortcode with the same name, and pass the attributes to the function. That way you can swap some of those attributes out to override them.

For example lets say we want to change the [toms_hello name="Tom"] shortcode:


add_shortcode( 'toms_hello', 'toms_hello_shortcode' );

function toms_hello_shortcode( $atts ) {
    return 'Hello ' . $atts['name'];
}

Lets say I want the name to always be “Jane Doe” no matter what the name says. I could do something like this:

remove_shortcode( 'toms_hello' );
add_shortcode( 'toms_hello', 'janedoe_toms_hello' );

function janedoe_toms_hello( $atts ) {
    $atts['name'] = 'Jane Doe';
    return toms_hello_shortcode( $atts );
}

This would be the fastest and most compatible solution.

4. Copy the shortcode under a new name

This is by far the easiest, copy paste the shortcode into a new plugin, and give it a new name, then make the adjustments you want. Note that you’ll need to make some adjustments to get it working that depend entirely on how the original was implemented.

A General Note

Shortcodes aren’t intended as a programming language, they’re just a way to do a substitution when you need to put PHP generated content inside a post. If you need
more complexity or you’re trying to plumb dynamic data into dynamic data, then you’ve moved beyond the usefulness of shortcodes, and are headed down a cul de sac of personal development.