In WordPress, we use plenty of plugins, Some of these plugins require us to place a shortcode in the post, like [favourite_posts]. Shortcode may be a good way to add functionality on our site, but when we deactivate the plugin, the shortcode in the post content don’t get removed.
What happens if it is not removed from the content ?
Shortcode will appear in the frontend in its unprocessed form and It will be printed there as it is. eg [favourite_posts]
if we are using shortcode in the places where data portability is not an issue then it’s okay.
Shortcodes may be used in a best way in following cases :
1. Use in Theme templates
2. Use in widgets
3. Use in theme settings
Removing unused shortcodes from the post content
Because we can’t remember each and every shortcode name used in the post content, It’s a very complex job to achieve this, but by the getting lists of active shortcodes and filter the content with this active shortcodes list and which are not exist in the list, remove those shortcodes from the content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function remove_unused_shortcodes($content) { $pattern = get_unused_shortcodes_regex(); $content = preg_replace_callback( '/'. $pattern .'/s', 'strip_shortcode_tag', $content ); return $content; } function get_unused_shortcodes_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; } add_filter('the_content', 'remove_unused_shortcodes'); |
Copy and paste this code to the end of current theme’s functions.php.
Note:
1. it doesn’t remove the shortcode permanently.
2. It will remove all content that are quoted between a ‘[‘ and ‘]’ tag.
Remove the shortcode by using database
1 | UPDATE wp_post SET post_content = REPLACE(post_content, '[shortcodename]', '' ) ; |
Replace “shortcodename” with the shortcode you want to remove and run this query in your site database.
But using this way, we can remove only particular shortcodes which name we know and it’s one by one process.
Hello, i use Shortcodes Ultimate in my site, but now i don’t use anymore and i want to delete all the old shortcodes because posts looks terrible. Can you help me about a plugin maybe wich can remove unused shortcodes? my site is drenusha.com
Hello @imiMoqmi
There is no such better plugin to remove unused shortcodes of Shortcode Ultimate. “Can you help me about a plugin…” <= Do you need any help about such existing plugin or need new one or any other query?
I checked your site and as it's not in English, I am unable to find unused Shortcode text there.
You can write here as well as contact us at http://fellowtuts.com/contact/ if you wish to stay private.
Thanks for asking
Bro thanks for replay, at this moment i use that plugin, but if i find any way to remove all shortcodes from that plugin i mean to deactivate it.
Here is example: http://www.drenusha. com/kepucet-ikonike-qe-ende-kane-sukses/
Hi
I checked the link and found that it’s not appearing as usual shortcode code (usually contains brackets). I think that we have to first inspect them how are they appearing and then perform some queries over database or tweaks in code.
If we can process then let me sure know. Also you can drop us mail at support@fellowtuts.com
Hi, thanks for this great article, that works excellent
but how about shortcode’s cloding tag?
e.g: [ / shortcode ]
Hi,
Thank you for asking. That is also done using regular expression & string replace function so we need bit time to create and test code. We will update you soon.