How to Remove Unused Shortcode From Posts [WordPress]

In WordPress, if you have used plenty of plugins, you will surely come across a few that require you to put a shortcode in the post, like [shortcode_1], for it to work. Using shortcode may be a good way to add functionality to your site, but when you deactivate the plugin, the shortcode in your content don’t get removed and it will appear in the frontend in its unprocessed form.

This is how it looks like in its unprocessed form:

shortcode-not-rendered

There are several ways to remove unused shortcode from your site:

If you know which particular shortcode you want to remove and you want to remove it permanently from your database, you can simply do a SQL query with the command below:

Replace “shortcodename” with the shortcode you want to remove.

Note: This is not a foolproof method because different shortcodes can come with different attributes and value, making it hard to form a catch-all SQL query.

If you know which shortcode to remove, but you don’t wish to remove it permanently, you can get it to render an empty value instead. Paste the following code at the end of the functions.php file (before the ?> tag) in your theme folder.

Replace the “shorcodename” with the shortcode that you want to remove.

Note: This is a good method only if you know which shortcode you are going to remove. You also have to make sure that the shortcode is no longer active, else it might results in conflict.

If you have a bunch of shortcodes that you no longer used and you can’t remember all the shortcode name, here is an ultimate way to disable all unused shortcode in your content.

Paste the code to the end of your functions.php just before the ?> tag.

What the above code does is to first retrieve the list of all active shortcodes, search the content for shortcodes that don’t match the active shortcode list and remove the shortcodes. It is modified from the get_shortcode_regex function that check for active shortcode.

Note: This method should work for all unused shortcode and it doesn’t remove the shortcode permanently. Next time you re-activate the plugin, its respective shortcode will continue to work automatically. On the flip side, this code will remove all content that are quoted between a “[” and “]” tag.

5 comments

  1. Thank you for the article, it’s almost exactly what I was looking for. I’m in the process of switching to another theme and there is a shortcode I want to remove.

    I’ve been using a plugin to embed tweets for a long time now (before oEmbed was implemented) and currently the shortcode looks something like this:

    [blackbirdpie url=”http://twitter.com/urltotweet”]

    Point 3 in your post suggest I remove the entire shortcode. But I need to keep the URL intact. So I was wondering. Is there a way for me to remove everything from that shortcode, except the URL?

    Thank you in advance for your answer.

    • If you only have one shortcode, you can try this function:

      add_filter(‘the_content’, ‘mte_remove_unused_shortcode’);
      function mte_remove_unused_shortcode($content)
      { $content = str_replace(‘[blackbirdpie url=”‘,”,$content);
      $content = str_replace(‘”]’,”,$content);
      return $content;
      }

  2. Hi Damien,
    I have a question and hope you can help me with a problem i’m facing…
    I use for my regular site a 2 column layout which is done by the shortcode:

    [two_third_column_left]
    text
    [/two_third_column_left]
    [two_third_column_left_last]
    text
    [/two_third_column_left_last]
    and styled by css.

    For the mobile version i use wp touch pro and want the columns to show up as accordion textbox. I reached this by the plugin ‘shortcode unlimited’ which need the shortcode:

    [accordion]
    [spoiler title =”Specifications”]
    text
    [/spoiler]
    [spoiler open=”1″ title =”Description”]
    Text
    [/spoiler]
    [/accordion]

    I thought it’s best to use two types of plugins (one for the regular site and one for the mobile output) but then i have to double declair the shortcode and remove one of them in one of the two outputs. In that case i deactivate the plugin in the regular or mobile site…

    When i do so i wonder which type of code-suggestions you gave is the best way to reach my wish.
    If you have other suggestions, it will be more then welcome.

    Thanks on behalf.
    Regards,

    Paul

    • Your case is complicated. Using two set of shortcode that serve the same purpose is not advisable as it could mess up your system. I still cannot figure out how can you deactivate the first plugjn while on mobile and reactivate it while on desktop?

  3. Tried this code and it works ok.
    But, it would be good if there is a way to keep content inside shortcode tags.

    For example:
    [two_column]Hi, this is inside two column text.[/two_column]

    Code will replace it with an empty string instead of striping the tags only.
    Any suggestions?

Comments are closed.

Sponsored Stories