0

First off to say I am not an expert wordpress or php but after some initial research I feel that this should be possible.

I want to adapt the behaviour of a plugin on my site (without altering the plugin source files) so that every time a shortcode runs then one of the attributes is replaced with information from user metadata. In this way the results are customised to user.

Fine to extract the user metadata but not sure on how I can override a specific shortcode attribute?

See Use content custom filter for all shortcodes Also Filter specific shortcode output? Also Add attribute to existing Shortcode

  • 1
    Do you have to hijack the existing one? You could just make a new one which takes the same parameters, does something with the parameters and then calls the existing one to let it do its thing. Or you could make one small change in the existing plugin to rename its shortcode, then write your new one with the existing name so you don't have to change it in your pages? – mozboz Jul 1 at 11:35
1

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.

| improve this answer | |

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.