3

Question

Is there a way to remove a specific shortcode, say [print_me] from all the posts where it appears in one shot. And I don't mean mask it or filter it so it doesn't show up while it stays in the post_content. I want it GONE GONE.

Attempts So Far ...

So far what I have done is using phpMyAdmin, I ran a query on all post_content that appears in the table wp_posts and it shows all the posts that I was looking for but there is too many to handle individually. I ran a find/replace update query on the results trying to replace it with "" (nothing) but while the query ran successfully, so it said, I don't see the shortcodes gone, so I am not sure if I missed something.

Here is the query I used to find them:

SELECT * FROM `wp_posts` WHERE `post_content` LIKE '%[print_me]%'

and I get 111 records. But then using the same tool to do a search and replace, I get:

Your SQL query has been executed successfully

UPDATE db1357924680.wp_posts SET post_content = REPLACE(post_content, '%[print_me]%', '%%') WHERE post_content LIKE '%%[print_me]%%' COLLATE utf8_bin

But when you check, they are all still there. SO I am hoping someone knows a better or proper way of doing this and willing to share. If I am on the right track, then I would appreciate some assistance in figuring out why its not working as expected. TIA.

Resolution (thanks to @TheDeadMedic)

Using the corrections to my original SQL query (as follows and found in the original answer), it worked flawlessly and I accomplished what I needed, thank you.

UPDATE db1357924680.wp_posts SET post_content = REPLACE( post_content, '[print_me]', '' ) WHERE post_content LIKE '%[print_me]%'


History (tl/rl)

I had been using a plugin for a while now that can make something appear on EVERY post and page or you could opt to only put the shortcode when you felt it was appropriate for that content.

However, after about 2 years, I gave it up in favor of a more wholesale solution that provided some other features that I wanted to incorporate in addition.

I have deactivated the plugin but now facing the problem of those shortcodes printing out as regular old text and that's something I need to resolve.

Some environment/system information: PHP/Linux/WP_4.5.1/SQL_5.1.73/PMA_4.1.14.8

  • Did you try the MySQL query AFTER disabling the plugin? – TomC Apr 29 '16 at 7:25
  • @TomC, thank you for checking but yes, of course ;) – GµårÐïåñ Apr 29 '16 at 18:23
6

For the permanent solution, your SQL query is slightly off - you need:

UPDATE db1357924680.wp_posts SET post_content = REPLACE( post_content, '[print_me]', '' ) WHERE post_content LIKE '%[print_me]%' 

MySQL replace example

  • 1
    ... and just remember to backup before playing with update sql queries ;-) – birgire Apr 29 '16 at 11:19
  • 2
    Backups are for losers ;) – TheDeadMedic Apr 29 '16 at 11:40
  • Thank you, I always backup before I mess with my db directly and its backed up weekly, so no worries, but thanks for the heads up. – GµårÐïåñ Apr 29 '16 at 18:26
  • As for the query, I see that you just removed the % which I guess were superfluous. Can you tell me why we need them during the search but not during the replace, or is that just for sake of that function works like this? Thank you again. – GµårÐïåñ Apr 29 '16 at 18:27
  • 1
    Because REPLACE treats % literally, whereas with LIKE they are wildcards (see linked documentation in answer). – TheDeadMedic Apr 29 '16 at 18:28
5

There is an easier way to do this:

add_filter( 'the_content', 'my_post_content_remove_shortcodes', 0 );

function my_post_content_remove_shortcodes( $content ) {

    /* Create an array of all the shortcode tags. */
    $shortcode_tags = array(
        'shortcode_1',
        'shortcode_2',
        'shortcode_3'
    );

    /* Loop through the shortcodes and remove them. */
    foreach ( $shortcode_tags as $shortcode_tag )
        remove_shortcode( $shortcode_tag );

    /* Return the post content. */
    return $content;
}

You can drop this in to your functions file.

Source: http://justintadlock.com/archives/2013/01/08/disallow-specific-shortcodes-in-post-content

--edit--

Instead of searching through your MySQL entries to remove everything, you can "remove" the shortcode's functionality through your functions file.

The code below will filter through all of your content and remove specific shortcodes that you designate in the place marked as shortcode_1 etc.

As Mark said: This is not quite what you asked for but will give you similar results if you struggle to find something else.

Hope it helps!

  • +1 as I agree that it makes more sense than what the OP wants :), but it isn't what he wants :) – Mark Kaplun Apr 29 '16 at 6:55
  • 1
    another thing, please try to avoid just dumping a code as an answer and try to explain what it does and why. Your code might not be obvious for people that dont' have much WP experiance – Mark Kaplun Apr 29 '16 at 6:57
  • 1
    You are of course correct, I've edited my answer to explain a little more :) Thank you Mark! – Frits Apr 29 '16 at 7:09
  • 1
    OP should consider that if the theme changes, then the shortcodes will appear due to missing code in new Theme's functions.php. May not be immediately obvious. However, I like @Frits answer. – TomC Apr 29 '16 at 7:23
  • @TomC, this is why non trivial shortcodes should be in a plugin and not a theme – Mark Kaplun Apr 29 '16 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.