How to hide a broken shortcodes in WordPress

by · April 29, 2015

Many plug-ins for WordPress shortcodes used to format the text to output forms and for different functionality. One of the common problems with shortcode deactivation occurs when such a plug, which leads to a shortcode open publication (or “bit”) form.

For example, instead of the feedback form, you will see in its place [contact–form]. Of course you can go through all the posts and pages manually and delete or change such a shortcode, but when we are talking about a large number of pages or shortcodes, that is far more efficient methods.

Hide Broken Shortcodes

Plugin Hide Broken Shortcodes created for solving this problem. It scans the message text in the posts and pages on the broken shortcodes and hides them, and it only happens in the output, and the original contents of the plug-in do not change.

Using this plug-in, be aware that if broken shortcode used around any text when it is hidden, the text remains visible. This approach is not entirely applicable to the shortcode, the purpose of which hide some of the text, for example to the user registration at the website:

1

2

3

[hide-unregistered]

Download link:...

[/Hide-unregistered]

If you need special handling for these shortcodes, we recommend that you do it with your own small plugin.

Own Plugin

To completely hide the contents of a specific shortcode in WordPress, it is enough to add a handler that returns an empty string. Auxiliary function __return_empty_string will make it nice one line:

1

add_shortcode('foo','__return_empty_string');

You can also hang on the shortcode handler other, if we know its function:

1

add_shortcode('foo','my_other_shortcode_handler');

If we do not have direct access to an outside processing functions shortcode, you can use thedo_shortcode () within our small processor. Note that this will be a little slower than the function call processing directly.

1

2

3

add_shortcode('foo',function($attr,$content=''){

returndo_shortcode('[my-other-shortcode]');

});

This approach allows us not only to replace the processing of a single shortcode to others, but also to control all attributes, and transfer them to the new handler.

For example, if our broken shortcode supported attributes width and height to specify the width and height, and the new shortcode support only attributes w and H , in our processor, we can write for this compatibility:

1

2

3

4

5

6

7

8

9

10

11

add_shortcode('foo',function($attr,$content=''){

$Attributes='';

if(!empty($attr['width']))

$Attributes.=Sprintf('w = "% d"',$attr['width']);

if(!empty($attr['height']))

$Attributes.=Sprintf('h = "% d"',$attr['height']);

returndo_shortcode("[my-other-shortcode {$ attributes}]");

});

For more complicated cases, Â you need to write the more complex logic, and sometimes it is not entirely justified. If shortcode used a couple of times – it is easier to find and manually replace.

How do you deal with beaten shortcode in WordPress? Leave your opinion in the comments, or emailÂ us at Twitter.