0

I need to find and replace text from all pages from a Wordpress site. This text shows in header as well, I tried this in functions.php but not working:

function replace_text($text) {
$text = str_replace('word1', 'word2', $text);

return $text;
}
add_filter('the_content', 'replace_text');

Anyone can help with this? Thanks

user2329017
  • 55
  • 1
  • 10
  • 1
    Where is the text? In a database, PHP or HTML file? What do you want to achieve, i.e. what do you want to happen when you find said text? – JustBaron Nov 20 '17 at 13:10
  • Ignore any comments / Non-WP answers saying you haven't provided enough info. Imo the issue is that you haven't added the priority argument to the end of your filter and something else is interfering with it. See this answer, add priority 99 https://wordpress.stackexchange.com/questions/28910/add-filter-the-content-str-replace-after-shortcode – McNab Nov 20 '17 at 13:38
  • @McNab what if this user wants to replace the word 'the' all over the website? It's not a good way to solve problems. The correct way is to find WHY is there any extra text and remove it in proper way. Author should provide us with more information. – pgndck Nov 20 '17 at 13:44
  • @McNab, I tried what you said but not working. – user2329017 Nov 20 '17 at 13:54
  • @unbirth - I agree with you, it depends on the use case. Ie for complicated filtering of `the_content` dom parsing - https://stackoverflow.com/questions/20820848/parsing-wordpress-post-content#answer-20822406 . The question here though is 'why is this not working?'. – McNab Nov 20 '17 at 13:56
  • Lol!! Well that shows me then, doesn't it. OK, you need to provide more information ;) . I'm off to eat some humble pie for my lunch :) – McNab Nov 20 '17 at 13:56
  • @JustBaron I want to find the text and replace it to another text. Text show in all pages from site in the html page source, for instance: – user2329017 Nov 20 '17 at 13:56
  • @user2329017 - OK, the text is in the source. Is it in the Wordpress post content though? `the_content` isn't filtering the whole page source, it specifically filtering the post/page content pulled from the database before it's rendered. – McNab Nov 20 '17 at 14:02
  • @McNab not in the post content, just in source – user2329017 Nov 20 '17 at 14:07
  • Well there's your problem. @JustBaron was absolutely correct. `the_content` ain't going to help you. It's in your theme somewhere then. – McNab Nov 20 '17 at 14:09
  • @McNab well thank you for your replies, I will go another way and just modify the content from DB, for this case it's all about the site title. Thank you all! – user2329017 Nov 20 '17 at 14:30

1 Answers1

-2

You have to pass exact parameters in call of function that are in function definition, call function like its name,

$data = "sdfsdfdsfwf"
$result = replace_text('find what you want to replace', 'replace with', $data);
echo $result;

Then in the definition of function name is same as the method call and parameters count is same as in call of method.

function replace_text($find, $replace, $data) {
   $text = str_replace($find, $replace, $data);

   return $text;
}

returned result contain replaced string.

want to know more about str_replace check this link

Ritesh Khatri
  • 1,253
  • 13
  • 29