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

fris fris at fris.net
Sun Mar 25 20:14:28 UTC 2012

Thanks for the reply, the sed you gave only removes the content inbetween
the shortcode so text here

I want to completely remove [shortcodename]content[/shortcodename] and also
parse for variables so [shortcodename] or [shortcodename id="12"] or
[shortcodename name="steve" color="black"]text[/shortcodename]

-----Original Message-----
From: wp-hackers-bounces at lists.automattic.com
[mailto:wp-hackers-bounces at lists.automattic.com] On Behalf Of mickey
Sent: Sunday, March 25, 2012 2:32 PM
To: wp-hackers at lists.automattic.com
Subject: [wp-hackers] best way to remove shortcode from post content

> 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
wp-hackers mailing list
wp-hackers at lists.automattic.com

More information about the wp-hackers mailing list