Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to replace a word on every page for example "Denver" with a word from a URL string, so I'll have like ?city=Atlanta in the URL. So I was thinking of using PHP's GET to just get the city from the URL string and str_replace to replace it, but which action/hook in WordPress do I have to attach it to?

The word will appear throughout the entire page, including title, logo description, content, footer, etc... so it can't just replace content only.

share|improve this question

2 Answers 2

Check the Filter Reference -- there are filters like the_content, the_title, wp_title, etc. I'm not sure what you'd filter to get the logo description and footer -- you might need to delve into your theme's code.

Also, make sure you sanitize anything you get from $_GET -- never ever trust user-generated content. See Data Validation for more information.

share|improve this answer
    
Dang I was hoping there would be a way to search all text and not have to go through individual elements. –  zen May 3 '13 at 15:28
1  
Each chunk of text -- the title, the content, etc -- is stored in a different field in the DB. But most everything in WordPress has a filter that you can use. Not all are documented in the Codex. Another site that lists filters and hooks -- though it doesn't explain them, by and large -- is Adam Brown's WordPress hooks database. –  Pat J May 3 '13 at 15:54

str_replace will make a mess of any unlucky markup on the page since it will replace matching text inside of markup or inside of URLs.

What you want is a modified versions of an answer I gave to another question about highlighting search terms. The change would be to the highlight_search_term function. You just need to alter it to use the $_GET data instead of the search data.

function highlight_search_term($text){
  if(!empty($_GET['city']){
    $keys = strip_tags($_GET['city']); // nominal validation
    $pattern = '/<[^>].*?>/i';
    preg_match_all($pattern,$text,$matches);
    $placeholders = array();
    foreach ($matches[0] as $v) {
      $placeholders[] = highlight_search_term_placeholders();
    }
    $text = preg_replace_callback($pattern,'highlight_search_term_cb',$text);
    $pattern2 = '/(' . $keys .')/iu';
    $text = preg_replace($pattern2, ' <span class="search-term">\1</span> ', $text);
    $text = preg_replace($placeholders,$matches[0],$text);
  }
  return $text;
}
share|improve this answer
    
Does this still need to be attached to each filter, the_content, the_title, etc...? Also, where would I put which word I want to replace? If the target word is "Denver", title tag might contain "The Denver Website", so I'd only want to replace "Denver". –  zen May 3 '13 at 15:26
    
Yes, it needs to be attached to the filters you want, at least. I only posted the altered code. You still need everything from the other answer. –  s_ha_dum May 3 '13 at 15:31
    
Gotcha. I'll play around with it a bit and will update the post. –  zen May 3 '13 at 15:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.