When I was working on schema data, I got an issue where a shortcode was executed within that data, which was not desired. So I looked for a way to disable this particular shortcode temporarily. I didn’t want to disable all shortcodes, which would have been easier by just removing the do_shortcode function within my code, because I was dependent on other shortcodes.

The idea

WordPress stores all registered shortcodes within the global variable $shortcode_tags. The idea was to remove the particular shortcode from this variable (which is an array with the name of the shortcodes as key), execute my code and then re-adding the shortcode again to make sure it works in the subsequent code. And this worked.

In practice

The code looks like this:

global $shortcode_tags;

// we don't want to execute the grw shortcode here
if ( isset( $shortcode_tags['grw'] ) ) {
	$ignored_shortcode = $shortcode_tags['grw'];
	unset( $shortcode_tags['grw'] );
}

// do stuff

if ( isset( $ignored_shortcode ) ) {
	$shortcode_tags['grw'] = $ignored_shortcode; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}
Code language: PHP (php)

In my case, the problematic shortcode was called grw, that’s why the key is named like this. If it is available, I store it in a dedicated variable and remove it from $shortcode_tags (lines 4 – 7). Then I execute my code that requires this shortcode to be removed (line 9) and re-add it later, if the dedicated variable has been set (lines 11 – 13).

Reposts

Leave a Reply

Your email address will not be published. Required fields are marked *

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Learn more about webmentions)