Remove shortcodes from WordPress posts

Shortcodes are pretty nifty things so WordPress plugins and themes often use them to add complex interactive elements to posts or pages with a single line of code.

But when you switch a theme or deactivate a plugin that relies on them the shortcode doesn’t just go away, instead you end up with weird bits of code on your posts and pages which can be troublesome to locate and remove depending on the size of your site.

Recently, we upgraded a simple eCommerce website to a powerful WooCommerce store and were tasked with removing the old plugin shortcode [wp_eStore_buy_now_button] from countless posts that would have been a mammoth task if done manually.

Instead, we added a shortcode of our own to the theme functions file that deleted them all instantly.

/**
 * Remove WordPress eStore shortcodes from posts
 */
function shortcode_remover($attrs=array()) {
    return '';
}
add_shortcode('wp_eStore_buy_now_button', 'shortcode_remover');

You can use this code to clear up shortcodes on your site as well. Simply replace ‘wp_eStore_buy_now_button’ with the shortcode you’d like to remove.