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

fris fris at fris.net
Sun Mar 25 21:21:40 UTC 2012


I actually did this and it seems to work

function remove_shortcode_from_db($shortcode = 'button') {

    global $wpdb;

    $posts = $wpdb->get_results("SELECT ID,post_title,post_content FROM
$wpdb->posts");
    $regex =
'/\[(\[?)('.$shortcode.')\b([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([
^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)/s';

    foreach($posts as $post) {
        $final = preg_replace($regex,'',$post->post_content);
        $change = $wpdb->update( $wpdb->posts, array('post_content' =>
$final), array('ID' => $post->ID));
    }
}

Regex is taken from the shortcodes include file.

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

what about \[shortcodename[^\]]*].*[^\]*]]

On 25 March 2012 21:14, fris <fris at fris.net> wrote:
> 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 
> panayiotakis
> 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\]
> or
> \[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"
> bar
> mickey$
>
> Egad I haven't seen a regex discussion in years.
>
> mickey
>
>
>
> --
>
> Mickey Panayiotakis
> Managing Partner
> 800.270.5170 x512
> <http://www.infamia.com>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



--
My Blog: http://pross.org.uk/
Plugins : http://pross.org.uk/plugins/
Themes: http://wordpress.org/extend/themes/profile/pross
_______________________________________________
wp-hackers mailing list
wp-hackers at lists.automattic.com
http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list