Best ways to remove unused shortcodes from WordPress site

In this article, I will explain to you how to remove unused shortcodes from the WordPress website.

WordPress 2.5 and later versions support the use of shortcodes in WordPress websites. The main advantage of using a shortcode is that it reduces the effort of doing various things. That is, it reduces the effort of creating the codes from scratch.

In reality, shortcodes are not always a good option because the shortcodes will take up a huge space in your database after some time.

You can use shortcodes through plugins or themes, but if you stop using any of them, the shortcodes you use will be stored in the database as huge. This can have a negative impact on traffic as it actually reduces the loading speed of the website.

It is imperative to remove the unused shortcodes from your WordPress site and also from the database. If you think so, then this article is just for you.

Below are methods that will help you in removing unused shortcodes from WordPress sites.

  • Adding code in ‘functions.php’
  • Use plugin to remove shortcode
  • Delete the code from the database
  • Hide from frontend

Adding code in ‘functions.php’

Suppose you have some unused shortcodes in your website which are not used or not usable in future. To remove them, follow the steps below.

Here I am using FTP client 'FileZilla' to edit the 'functions.php' file. Same as below.

Here is the file path: Open the root or your website folder > wp-content > themes > open currently used theme folder > functions.php.

Now paste the below code in the ‘functions.php’ file above the last tag ?>.

add_filter('the_content', 'mte_remove_unused_shortcode');
function mte_remove_unused_shortcode($content)
{ $pattern = mte_get_unused_shortcode_regex();
$content = preg_replace_callback( '/'. $pattern .'/s', 'strip_shortcode_tag', $content );
return $content;
}

function mte_get_unused_shortcode_regex() {
global $shortcode_tags;
$tagnames = array_keys($shortcode_tags);
$tagregexp = join( '|', array_map('preg_quote', $tagnames) );
$regex = '\[(\[?)';
$regex .= "(?!$tagregexp)";
$regex .= '\b([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)';
return $regex;
}

Once you paste this code into 'functions.php', it tells WordPress to remove all shortcodes. This method may look difficult for beginners, moreover, it may affect the whole process of your website.

Use Plugin to remove shortcodes

You know that in WordPress, every little process has many plugins. There are also many plugins for removing shortcodes. I recommend you install the plugin 'Hide Unwanted Shortcodes'. This plugin will help you to hide unwanted shortcodes on your website.

To use this plugin, you should remember the shortcodes on your website. However, only the shortcodes mentioned by the plugin will be removed, but other shortcodes will remain on your website. I will not recommend this method because if you do not remember the shortcodes, this plugin is not useful.

Delete from Database

This method also requires a shortcode name to remove the shortcode from the database. I will recommend a simple query to remove shortcodes. The following query is as follows,

UPDATE wp_post SET post_content = replace(post_content, '[shortcode]', '' ) ;

Login to your PHP admin and go to the admin section. Now select the database and run the above code. Mention the shortcode name in the 'shortcode' place.

Hide from frontend

Apart from using the plugin to remove the shortcodes. I recommend you to use this method which will hide all shortcodes from your website. You just need to add the following code in the 'functions.php' file.

add_shortcode( 'shortcode', '__return_false' );

Before you specify the shortcode name in the code, make sure that the shortcode is not currently active.

Summary

We hope this article will help you to remove the unused shortcodes from your live website. Basically, you need to know the shortcode name to remove the shortcodes from your database. Remember that the shortcodes should be inactive before removing them.

Hope you like our post how to remove unused shortcode from WordPress. You can follow us on Facebook and Twitter to get the latest updates.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.