Post Pic

WordPress tip: Get rid of unused shortcodes

WordPress shortcodes are great, but they have a bad point: When you decide to stop using them, their code stay in your post content. Here is a fix: A simple SQL query to run on your database to get rid of unused shortcodes.

Simply run the following SQL query on your WordPress database, using the command line client or PhpMyAdmin. In this example, I assume the unused shortcode is [tweet].
Don't forget to backup your database before using this query.

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

If you like to know more about WordPress SQL queries, you should have a look to this article.

8 Responses

Mar 15 2010 17:24

Very useful tip! Thanks!

By the way… ‘XordPress’ sounds awesome. :)

Mar 16 2010 12:48

Thanks! Nice tip.

However, performance-wise, the query is not optimized. It will go through every single post to replace the shortcode, even if it’s not in the post content. This can be a bit slow.

A better one would be:
UPDATE wp_post SET post_content = replace(post_content, ‘[tweet]‘, ” ) WHERE post_content LIKE ‘%\\[tweet\\]%’;

The double slashes are for escaping those square brackets, otherwise MySQL will misunderstand [tweet] as a character class in a pattern.

Mar 19 2010 01:14

Thanks for this Jean. And fix the typos (XordPress and SQl – that L looks like I), someone could get confused :)

Mar 19 2010 14:35

Great tip !

Mar 21 2010 16:55

Great article, thanks.

Mar 27 2010 09:46

Thanks, another useful tip. I love your site and keep all your emails in one place so I can return to them as and when I need to.

Brill ;)
Regards
Micheal

Jun 22 2010 23:25

Hey, I ran that code on a clients blog today to fix broken links after moving the WordPress installation: It worked, fixed all broken URLs.

(btw: I had to rename wp_post to wp_posts so it worked).

Thank you!

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required