1

I am looking to replace a few text strings for only two specific pages on a wordpress site. It should not affect those strings on any other pages. I'd prefer to do this via adding some code to functions.php

I think using part of this code would be the first part, just need help with the rest https://wordpress.stackexchange.com/a/278357

3
  • 1
    Welcome to WPSE. You need a little more context in your question. Based on the link it sounds like you want to change some translation strings? If so, you should specifically state that in your question. Also, a relevant example (besides just a code snippet from another question) would be helpful for anyone trying to provide an answer. That would give some context to your question. Commented Nov 20, 2018 at 1:24
  • 1
    You could add a filter to the_content, but it depends if you want to translate, replace the output or replace the saved content. Commented Nov 20, 2018 at 2:59
  • please check this link: wp-mix.com/replace-all-instances-string-wordpress Commented Nov 20, 2018 at 4:15

3 Answers 3

1

I have run through the same issue from past 18 hours. I had tried multiple permutations and combinations of codes and finally, this worked for me. Put this code in the Appearance >> Theme Editor >> functions.php fie:

function replace_text( $text ) {
    if( is_page( 1709 ) ) {
       $text = str_replace('old_text', 'new_text', $text);
       $text = str_replace('old_text', 'new_text', $text);
       }

return $text;
}

add_filter('the_content', 'replace_text');
  • old_text is the text you want to replace and new_text you want to replace it with.
  • To determine the id, please edit the page, you will find the id in the URL.
1

I can't comment so I'll answer and slightly adjust Bhagyashree's which is exactly what you'll need to do based on your question. Except you may want to know how to include the 2 pages rather than duplicating the function. And also passing in an array of strings to replace.

function replace_some_text( $content ) {
   
    // Notice the || we're saying if is page1 OR page2. Also changed to is_single.
    if( is_single( 2020 ) || is_single( 2021 ) {

       $text_strings_to_replace = array( 'first_string', 'second_string', 'third_string_to_replace' );

       $content = str_replace( $text_strings_to_replace, 'new_text', $content );
       
}

       return $content;
}

add_filter('the_content', 'replace_some_text');

Little explanation. Basically, we hook into a WordPress filter hook [add_filter][1] that allows us to filter all the content on the page.

We filter the $content inside our function, in this case using a PHP function [str_replace][1] to search the content for our array of strings and replace them with 'new_text'.

1
  • The solutions proposed above did not work for me unfortunately, apparently because in such case the WooCommerce plugin overrides a function in functions.php. Thus the solution for me was using the Say What plugin. Commented Jul 28, 2024 at 21:56
0

Some Example Here is how to replace all instances of a string in WordPress. Just add the following code to your theme’s functions.php file:

function replace_text($text) {
    $text = str_replace('look-for-this-string', 'replace-with-this-string', $text);
    $text = str_replace('look-for-that-string', 'replace-with-that-string', $text);
    return $text;
}                                                                                           
add_filter('the_content', 'replace_text');

More info on http://php.net/manual/en/function.str-replace.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.