Dear Customers, due to our National Holiday, WP Kraken will be unavailable on 6 and 7th January, all quotes will be immediately responded on Monday 10th

How to get rid of non-existent shortcode from WordPress content

Lots of us already had problems with left overs from not working shortcodes. Most likely you use some great plugin that helps you with creating/extending your content. Let’s say it’s a social buttons extension. You’ve added the provided shortcode to your posts and pages. After some time you stumble upon a different plugin that looks better or has more cool features that you would like to use on your website. When you disable the old plugin, the shortcode you previously used suddenly stops working. Now,in the place of social buttons you can see something like this:
[k_blog_code] [easy_social_buttons] [/k_blog_code]

Seeing this text will be probably a bit confusing to your readers, so it’s best to get rid of it. And that’s the hardest part. Normally, you would probably go through every post and page to check if you’ve added the previous shortcode or not. Then you would switch to edit view and try to find the shortcode in post content, remove it and finally save the post.
Luckily, I’ve got good news for you. We know of few methods and tricks that will help you to speed up the process significantly.

Hide the shortcode

The easiest way of removing the shortcode that isn’t working is replacing it. To do this we will need to… create our own shortcode (isn’t that rad?). We will use the [easy_social_buttons] as an example. To hide it on our pages we will create a shortcode with the same name. Thanks to that every place that has this shortcode will use our replacement and won’t display the shortcode as some strange texts in brackets.

This technique of creating shortcode will hide it with all texts and attributes. To use this method just paste to your functions.php code displayed below and change the shortcode name to the one you want to remove.
[k_blog_code]// Remove shortcode
add_shortcode( ‘easy_social_buttons’, ‘__return_false’ );[/k_blog_code]It’s that easy!
If you do not want to get your hands dirty with the code you can use a plugin made by Meks, it’s called Remove Orphan Shortcodes. It works in the same way as the script above. In my option, it is better to just add the code yourself. Bundling to many plugins is not the best way to maintain your website.

Display only the content

Unfortunately, that’s not always the best way. In our example, we had some really basic shortcode without any content. Let’s say you used another shortcode that was highlighting the important phrases in your posts. Something like [fancy_highlight]keyword for current paragraph[/fancy_highlight]. You don’t want to remove it because your articles will have empty spaces in the places where it should have the most important parts. We can use the second approach of creating a shortcode that won’t remove content between. Like with the first one just use below function and change the name of shortcode to the one you want to replace.[k_blog_code]// Remove shortcode but leave its content on page
function wpkb_extract_shortcode_content( $atts, $content ) {
return do_shortcode( $content );
}
add_shortcode( ‘fancy_highlight’, ‘wpkb_extract_shortcode_content’ );
[/k_blog_code]The difference between this and the previous one is only the output. You will have shortcode content on your page without the shortcode tags. “Example sentence with [fancy_highlight]very important keywords[/fancy_highlight] in the middle” will change to “Example sentence with very important keywords in the middle”. It’s much better than “Example sentence with in the middle”. You will lose the highlight effect but the sentence will still make sense for your visitors. If you have more than one shortcode just duplicate the line with add_shortcode() function and change the shortcode name to the other shortcode you want to hide.

Shortcode finder

The two methods are great and easy but they are not the best. Removing not working shortcodes from posts by editing the content was the way to go for long time website or blog. I know it’s the most tedious method but we need to put a little extra effort for the sake of our website speed. It won’t be made automatically but we can help you make it much much easier. We will need to create a shortcode that searches for other shortcodes. That’s quite funny but luckily for us, we can remove it after the search and us it whenever we need it again.

Paste the below code to your functions.php as previously.

[k_blog_code]// Shortcode for finding shortcodes in posts
function wpkb_find_posts_with_shortcode( $atts ) {
$atts = shortcode_atts( array(
‘name’ => ”,
), $atts );

if ( $atts[ ‘name’ ] != ” ) {
$query = new WP_Query( array(
‘s’ => ‘[‘ . $atts[ ‘name’ ],
‘post_type’ => ‘any’,
‘posts_per_page’ => -1,
) );

if ( $query->have_posts() ) {
$content = ‘<strong>List of posts with shortcode “‘ . $atts[ ‘name’ ] . ‘”:</strong>’;
$content .= ‘<ul>’;

while ( $query->have_posts() ) {
$query->the_post();

$content .= ‘<li>’ . get_the_title() . ‘ ( <a href=”‘ . get_the_permalink() . ‘” target=”_blank”>View</a> | <a href=”‘ . get_edit_post_link() . ‘” target=”_blank”>Edit</a> )</li>’;
}
wp_reset_postdata();

$content .= ‘</ul>’;
} else {
$content = ‘<p><strong>Sorry no posts with shortcode name “‘ . $atts[ ‘name’ ] . ‘” found.</strong></p>’;
}
} else {
$content = ‘<p><strong>Please set shortcode “name” for search.</strong></p>’;
}

return $content;
}
add_shortcode( ‘find_posts_with_shortcode’, ‘wpkb_find_posts_with_shortcode’ );[/k_blog_code]
This will add helper shortcode for finding all posts with the shortcode you want to remove. To use it add this to one of your pages and click Preview button or create a new page just to search and remove the shortcode.

[k_blog_code] [find_posts_with_shortcode name=”easy_social_buttons”][/k_blog_code]
When you view the page it should display a list of all posts that have the shortcode you are searching for. The list will have post name and two links after that:
View – you can check the post in a new tab and see where the shortcode is used.
Edit – you can go directly to edit screen in WordPress admin panel to remove the shortcode.

To make it easier to use our finder shortcode displays additional messages. If it can’t find any post with searching shortcode it will let you know about it. Also if you forget to specify the name of shortcode you want to find it will display a little reminder.

Conclusion

To sum up a little word of advice: shortcodes are great if used properly. It’s hard to imagine creating WordPress websites without them. As with everything – it’s good to not overuse them and have few dozens of plugins with multiple shortcodes. This will most likely decrease the speed of your website and may impact your traffic and page rank.

Adrian W.
Adrian W.
This website uses cookies to ensure you get the best experience.