Assume we have some plugin called AAA and it has built-in shortcode aaa_content and its usage is like this:
[AAA_content title=”Recent news” id=”23″]
Now we want to change its output HTML.
In WordPress, the output of any 3rd party function can be changed only if it has apply_filters() filtered output. Unfortunately not all functions and shortcodes have such filter in output. So it is almost impossible to modify its output without editing the plugin’s source file.
And as we know, editing the plugin file is always bad idea. The changes will be erased just in the next update.
So we have some different approach for such cases.
For example, here is one idea, which is subject of this blog post.
Creating parent shortcode which handles the output of the original shortcode.
So let’s declare and build that alternative shortcode.
add_shortcode('custom_AAA_content', function($atts){
$output=do_shortcode('[AAA_content
title="'.$output["title"].'" id="'.$output["id"].'"]');
//here we can modify the output. for example replace London with New York.
$output=str_replace("London","New York",$output);
return $output;
});
That’s all. Now we can just replace [AAA_content …] with [custom_AAA_content …] anywhere in the website. And we will get modified output without needing editing the source code of the original plugin.