PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 2, 2022

[FIXED] Plugin development - Revised shortcode attribute name as part of update

 February 02, 2022     plugins, posts, shortcode, tags, wordpress     No comments   

Issue

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"]');



Answered By - Miguel Angel Martinez
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing