• i need replace words in my posts and saved new words after delete code in my functions

    i use this code for replace

    function replace_text_wps($text){
    $replace = array(
    // ‘WORD TO REPLACE’ => ‘REPLACE WORD WITH THIS’
    'wp' => '<img href="#"><a href="#">wordpress</a>',
    
    );
    $text = str_replace(array_keys($replace), $replace, $text);
    return $text;
    }
    
    add_filter('the_content', 'replace_text_wps');
    add_filter('acf/load_value/name=artist', 'replace_text_wps');

    this code is ok and replaced my words but if delete this code in my functions new words not saved

    i want one time replaced all words and save and delete code

    how to that this ?

    sorry , i can’t good speaking english

Viewing 8 replies - 1 through 8 (of 8 total)
  • This is because the text is replaced on the fly at the point the_content filter is called.

    For permanent replacement you’d want to try a tool like this that does a DB wide search and replace.

    https://interconnectit.com/products/search-and-replace-for-wordpress-databases/

    Don’t forget to take backups, before and after

    I’d sugget that you look at the save_post action, and use that to trigger a replacement in the actual text.

    https://codex.www.ads-software.com/Plugin_API/Action_Reference/save_post

    Just remember to take a very careful look at the part about avoiding infinite loops… that’s got me a few times before!

    Thread Starter alam7o

    (@alam7o)

    By changing the code gets the job done function ????

    function replace_text_wps($text){
    $replace = array(
    // ‘WORD TO REPLACE’ => ‘REPLACE WORD WITH THIS’
    'wp' => '<img href="#"><a href="#">wordpress</a>',
    
    );
    $text = str_replace(array_keys($replace), $replace, $text);
    return $text;
    }
    
    add_filter('the_content', 'replace_text_wps');
    add_filter('acf/load_value/name=artist', 'replace_text_wps');
    Moderator bcworkz

    (@bcworkz)

    Catacaustic is right, ‘save_post’ action will work to change the content of posts right after they are saved.

    You could also use the ‘wp_insert_post_data’ filter. IMO this is even better than ‘save_post’ even though it too is perfectly acceptable. This way you can alter the data before it is saved instead of needing to re-save it and possibly starting an infinite loop if you are not careful.

    In both cases, you cannot use the same callback for the ‘acf/load_value/name=artist’ filter because the content is passed differently. If this filter is like ‘the_content’ which acts on outgoing data, it would be inappropriate anyway.

    You can alter ACF fields by getting the saved data from the post_meta table and doing the search/replace routine, then save the data back. To do this you need the post ID, so in this case the ‘save_post’ action would work better. Your callback does need to run after ACF saves the data itself. I’m not sure how ACF hooks into the save post process to do its part. To ensure your callback runs as late as possible, hook the ‘wp_insert_post action’ instead of ‘save_post’. It fires a tiny bit later. Also supply a large number as the priority argument of your add_action() call to help ensure your callback runs last.

    One last thing. It’s conceivable your search/replace routine could erroneously replace a “wp” occurrence that’s contained within a word like “towpath”. To avoid this, either use a placeholder that uses special characters like “?wp?” or use preg_replace() where the regexp includes whitespace tokens so only “wp” appearing as a separate word would be matched.

    • This reply was modified 8 years, 5 months ago by bcworkz.
    Thread Starter alam7o

    (@alam7o)

    @bcworkz i add this code but not worked to me

    function actualiza() {  
    global $post;  
    $args = array(  
        'numberposts' => -1,  
        'post_type' => 'post',  
        );  
    $the_query = get_posts( $args );  
    if ($the_query) {  
        foreach ($the_query as $post) {  
            $name = get_post_meta($post->ID, 'artist', true);
                 update_post_meta($post->ID, 'artist', $name );
        } 
    }
    }   
    wp_reset_query();  
    add_action('wp_head', 'actualiza');
    Moderator bcworkz

    (@bcworkz)

    What are you trying to accomplish with this? As it stands, your code gets a value and saves it back to the same place it came from, thus nothing is changed.

    Even if it did accomplish something, the code will not scale well. When you have a large number of posts it’s going to start dragging down page load times.

    Thread Starter alam7o

    (@alam7o)

    i want just re-saved data . not change just re-save

    Moderator bcworkz

    (@bcworkz)

    Why re-save the same data? Why not just let it be? Are you trying to trigger some action or filter that accomplishes something even though the data does not change? If so, look at calling do_action() or apply_filters() to do this. Look at the source code for update_post_meta() to see what actions are fired.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘replace words in my posts and saved new words’ is closed to new replies.