Search code examples
wordpresspluginstagsshortcodeposts

Plugin development - Revised shortcode attribute name as part of update


In a plugin I am coding and supporting, I have revised a shortcode attribute recognised by the plugin to a name which makes more sense to a user.

I want to update every post where the attribute is used. I'm guessing it's not sensible to run code to update users' pages and posts by going through each one, and where found, changing the attribute from the old one to the new one. That seems very dangerous to me.

An option I know I have is to add the filter do_shortcode_tag, so every time the shortcode is called, I can treat the old attribute the same as the revised one. It's a tiny overhead I know, but literally speaking it is just a patch.

I'm thinking I can't be the only one faced with something like this. Ideally, I'd be able to run an action, or something available from the core to update relevant posts. Does anyone know of a solution?


Solution

  • It could be something like this:

    add_shortcode('sample_shortcode', 'render_sample_shortcode');
    
    function render_sample_shortcode($attrs)
    {
        $attribute = isset($attrs["newattr"]) && !empty($attrs["newattr"]) ? $attrs["newattr"] : $attrs["oldattr"];
    
        return "Iam reading this attribute value: " . $attribute;
    }
    

    Your shortcode:[sample_shortcode newattr="I am new" oldattr="I am old"]

    Or in a PHP file: echo do_shortcode('[sample_shortcode newattr="I am new" oldattr="I am old"]');