Search and replace text across all posts

I had used the ZD Video Plugin for WP a long time, and now, since my move to WP 3.0.2, it doesn’t work anymore. To embed a YouTube video using the plugin, I would just write

[zdvideo]url-of-youtube-video[/zdvideo]

Read More

Now I need to go back to plain YouTube embedding. How do I write a MySQL query that searches for [zdvideo]*[zdvideo] and replaces it with *?

Related posts

Leave a Reply

4 comments

  1. What about these two queries?

    UPDATE wp_posts SET post_content = REPLACE(post_content, '[zdvideo]', '');
    UPDATE wp_posts SET post_content = REPLACE(post_content, '[/zdvideo]', '');
    
  2. Best I’m aware, MySQL doesn’t have much of a regexp replace functionality — not to mention its very clunky regexp syntax. So this is easiest to do at the php level. Start by fetching all posts that have the shortcode:

    $posts = $wpdb->get_results("
    SELECT ID, post_content
    FROM $wpdb->posts
    WHERE post_content LIKE '%[zdvideo]%'
    ");
    

    And then loop through the result:

    foreach ($posts as $post) {
      $post->post_content = preg_replace(
        "/\[zdvideo\](.+?)\[\/zdvideo\]/",
        "$1", # $1 holds the url... format as needed
        $post->post_content);
    
      # Be sure to verify on a few posts before actually saving...
      # var_dump($post->post_content);
    
      $wpdb->query("
      UPDATE $wdpb->posts
      SET post_content = '" . $wpdb->escape($post->post_content) . "'
      WHERE ID = " . intval($post->ID)
      );
    }
    
  3. Try with wp search-replace.

    Searches through all rows in a selection of tables and replaces
    appearances of the first string with the second string.

    WP-CLI is the command-line interface for WordPress. You can update plugins, configure multisite installations and much more, without using a web browser.