WordPress.org

Ready to get started?Download WordPress

Forums

Changing String in All Posts (9 posts)

  1. ashes999
    Member
    Posted 8 years ago #

    If I have a string in multiple posts--say the string "ABCDEFG", is there a way that I can do a post-wide search-and replace, to replace it with something else?

    If yes, then can this also be done at the HTML level?

  2. katesgasis
    Member
    Posted 8 years ago #

    You need to have a plugin to do that. Use regular expression to loop over the posts in a function called by a post_content hook.

  3. davidchait
    Member
    Posted 8 years ago #

    try searching on search and replace. I think this has been discussed before. you sound like you want a one-shot update of content, not a plugin that converts every time the content is loaded.

    -d

  4. Aleister
    Member
    Posted 8 years ago #

    Here is a basic plugin:

    <?php
    /*
    Plugin Name: Plugin name goes here
    Plugin URI: The plugin's URI goes here
    Description: Description goes here
    Author: Name goes here
    Version: Version goes here
    Author URI: Your URI goes here
    */

    function replace_text_in_posts($content) {

    return str_replace("text to find", "text to replace it with", $content);
    }

    add_filter('the_content', 'replace_text_in_posts');

    ?>

    But yes, as davidchait mentioned, if this is simply a one time thing, you could always do something like an SQL query to replace the text.

  5. ashes999
    Member
    Posted 8 years ago #

    Yes, I'm looking for a one-shot change. Is there a simple way to do it? If not, I can probably figure out an SQL statement that'll cover the changes for me.

    Something like:

    UPDATE wp_posts SET post_content <something> WHERE post_content LIKE (SELECT post_content FROM wp_posts WHERE post_content LIKE <search string>)

  6. Chris_K
    Member
    Posted 8 years ago #

    You saw the post immediately above yours?

  7. ashes999
    Member
    Posted 8 years ago #

    Yep. Not bad for dynamic search/replace, but not what I want.

  8. Aleister
    Member
    Posted 8 years ago #

    After a bit of searching, I found this plugin:

    http://thedeadone.net/?p=183

    It gives you a new panel, with 'search and replace', and will go through and do an SQL replace of text.

    It is a plugin, but not in the sense that it will execute on all posts. It simply gives you a form in your admin panel which lets you do a SQL search and replace. It does not need to run on all posts because it does it directly to the database.

    I will have to add this to my list of useful plugins :)

  9. ashes999
    Member
    Posted 8 years ago #

    Thanks, that's positively brilliant :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.