How to Find and Remove Unused Shortcodes From WordPress Posts

By WP Saviour •  Updated: 08/11/20 •  3 min read

Shortcodes are good, nevertheless they are not on a regular basis the simplest approach. One downside of using a plugin or theme that depends upon shortcodes is that after you switch a theme or deactivate the plugin, they could go away behind shordcode tags in your posts which is ready to look uncommon to your readers. On this text, we’re going to current the way in which to find and take away unused shortcodes out of your WordPress posts and pages.

Are Shortcodes Unhealthy?

No, utterly not. Shortcodes aren’t harmful, nevertheless overusing them may be problematic. As an illustration, we use Compact Archives plugin which provides a shortcode and a template tag. Now we’ve the shortcode solely on our archives net web page, so if we ever deactivate that plugin, then there is just one net web page we’ve to remove the shortcode from.

Alternatively, there are plugins and themes that current shortcodes to create widespread mannequin parts like buttons, tables, columns, and so forth. Some ad management plugins moreover use shortcodes. Now if an individual has used these shortcodes in a lot of posts, then it turns into very powerful for the individual to remove the shortcode from all posts and pages.

For that reason we recommend our clients to not rely on themes or plugins which require you in order so as to add shortcodes into many posts. It’s best to on a regular basis try to find the next completely different for many who can, or contact the theme or plugin author. They might inform you a higher strategy to get the equivalent efficiency with out using too many shortcodes in posts or pages.

For these nonetheless questioning, once you’ve acquired an inactive shortcode in your web site, then it’ll look like this throughout the middile of your content material materials:

[some-random-shortcode]

With a function to remove unused shortcodes out of your posts and pages, you wish to first uncover them.

Uncover All Posts Containing a Particular Shortcode

We’re going to attempt the perfect technique to look out the shortcode inside submit content material materials. Merely copy and paste the subsequent code in a site-specific plugin or your theme’s capabilities.php file:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

carry out wpb_find_shortcode($atts, $content material materials=null) {

ob_start();

extract( shortcode_atts( array(

        'uncover' => '',

    ), $atts ) );

$string = $atts['find'];

$args = array(

    's' => $string,

    );

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {

        echo '

    ;

        whereas ( $the_query->have_posts() ) {

        $the_query->the_post(); ?>

        

WP Saviour