[wp-hackers] best way to remove shortcode from post content

mickey panayiotakis mickey at infamia.com
Sun Mar 25 18:32:13 UTC 2012

> From: "fris" <fris at fris.net>
> Subject: [wp-hackers] best way to remove shortcode from post content
> I know I could create/modify a shortcode and just return null content, but
> I
> was hoping for a better solution via mysql, or some other plugin.
> Found this from
> http://kovshenin.com/2011/regex-replace-in-mysql-or-lib_mysqludf_preg-in-ubu
> ntu-linux/
> what would be the best way to remove shortcodes with variables like
> [shortcodename id="12"]text here[/shortcodename]
> this?
> /(.?)\[(shortcodename)\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)/
I'm a big fan of simplifying regex. I think a simpler regex would be

\[shortcodename id=\"12\"\](.*)\[\/shortcodename\]

If the id changes:
\[shortcodename id=\"[0-9]+\"\](.*)\[\/shortcodename\]
\[shortcodename id=\"[^"]+\"\](.*)\[\/shortcodename\]

How you use these is up to you.  My 2 cents: If this is a one-off, I'd
consider using mysqldump and vi or sed to modify the dbdump rather than
compiling and installing installing UDFs. Something like this:

mysqldump -flags your_database | sed s/\[shortcodename[
]?.*\](.*)\[\/shotcodename\]/\1/g  >new_sql

Here's a test with some escaped characters for sed and shell
mickey$ echo '[shortcodename id="foo"]bar[/shortcodename]' | sed
"s/\[shortcodename[ ]*[^]]*\]\([^[]*\)\[\/shortcodename\]/\1/g"

Egad I haven't seen a regex discussion in years.



Mickey Panayiotakis
Managing Partner
800.270.5170 x512

More information about the wp-hackers mailing list