Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggreagtion Enquires Welcome

Find and Replace Words in a WordPress Post

A short time ago we shared the Pirate Day WP Shortcode. In itself the code was kind of useless... but it can be applied in the marketing world to increase upon your conversions. What follows is just two variations upon the pirate theme: a location-based find-and-replace, and an affiliate-style (or advertising keyword) find-and-replace. A full-featured tooltip WordPress plugin is forthcoming.

Geographic Specific Text

If your business has multiple offices and/or locations, you might consider creating a number of $patterns arrays that contain office-specific information, and then parsing the content based on geographic location. The following examples retrieves the user IP address, extracts the state, and then compares it against our array (which may include as much information as you like). We've used just three Australian states for the purpose of the demonstration.

The first function is required in company with the next. It resolved the user IP to a state and then caches the result (the Simple Cache WP plugin and the beliefmedia_ip() function are required).

1
<?php 
2
/*
3
    IP To State : http://www.beliefmedia.com/ip-location-api
4
    Requires: http://www.beliefmedia.com/simple-php-cache & http://www.beliefmedia.com/get-ip-address
5
*/
6
 
7
 
8
function beliefmedia_geo_state($ip = '', $cache = 5184000) {
9
 
10
  /* http://www.beliefmedia.com/get-ip-address */
11
  if ($ip == '') $ip = beliefmedia_ip();
12
 
13
  $ip_transient = 'ip_state_' . md5(serialize(func_get_args()));
14
  $cachedresult =  beliefmedia_get_transient($ip_transient, $cache);
15
 
16
  if ($cachedresult !== false ) {
17
   $result = $cachedresult;
18
 
19
   } else {
20
 
21
     $data = file_get_contents('http://api.beliefmedia.com/geo/json.php?ip=' . $ip);
22
     if ($data !== false) $data = json_decode($data, true);
23
     $result = ($data['status'] == '200') ? trim($data['data']['regionname']) : 'Unknown'; /* State */
24
     beliefmedia_set_transient($ip_transient, $result);
25
 
26
  }
27
 
28
 return $result;
29
}

Once you have a location, you can serve geographically relevant content with the following WordPress filter.

1
<?php 
2
/*
3
    Pirate Day WordPress Shortcode (example code)
4
    http://www.beliefmedia.com/pirate-day-wordpress
5
*/
6
 
7
 
8
function beliefmedia_change_text($content) {
9
 
10
   /* Example code to get state */
11
   $state = beliefmedia_geo_state();
12
 
13
   /* Array for each preferred location */
14
   switch ($state) {
15
 
16
       case 'New South Wales':
17
           $patterns = array(
18
             '/%%name%%/' => 'Marty',
19
             '/%%city%%/' => 'Sydney',
20
           );
21
           break;
22
 
23
       case 'Queensland':
24
           $patterns = array(
25
             '/%%name%%/' => 'NotMarty',
26
             '/%%city%%/' => 'Brisbane',
27
           );
28
           break;
29
 
30
       case 'Victoria':
31
           $patterns = array(
32
             '/%%name%%/' => 'SomeStooge',
33
             '/%%city%%/' => 'Melbourne',
34
           );
35
           break;
36
 
37
       case 'Unknown':
38
           $patterns = array(
39
             '/%%name%%/' => 'Martin',
40
             '/%%city%%/' => 'Sydney',
41
           );
42
           break;
43
 
44
       default:
45
           $patterns = array(
46
             '/%%name%%/' => 'Martin',
47
             '/%%city%%/' => 'Sydney',
48
           );
49
           break;
50
   }
51
 
52
   /* Search and destroy */
53
   foreach ($patterns as $pattern_search => $pattern_replace) {
54
     $content = preg_replace($pattern_search, $pattern_replace, $content);
55
   }
56
 
57
 return $content;
58
}
59
add_filter('the_content', 'beliefmedia_change_text');

When writing in your text editor simply replace a name with %%name%% and a city with %%city%%.

Find and Replace Words in a WordPress Post

Example of how the find-and-replace text is used.

If your IP address resolves to any location other than New South Wales, Queensland, or Victoria, you'll see the default text (my details).

Marketing Links

Replacing words in text can have any number of marketing applications. If you're an affiliate marketer, you can apply changes to keywords reactively and alter all occurrences of a word to an affiliate link. If you 'lease' keywords to advertising partners you can apply the keyword-links over a specific timeframe.

1
<?php 
2
/*
3
    Find and Replace Words in a WordPress Post
4
    http://www.beliefmedia.com/wordpress-find-and-replace
5
*/
6
 
7
 
8
function beliefmedia_replace_custom_word($content){
9
   $replace = array(
10
     '/\sWordPress\s/' => ' <a style="color:sienna; border-bottom: 1px dotted #E67300;" href="http://www.beliefmedia.com/" title="BeliefMedia">WordPress</a> ',
11
     '/\smarketing\s/' => ' <a style="color:sienna; border-bottom: 1px dotted #E67300;" href="http://www.martinkhoury.com/" title="MartyKhoury">marketing</a> ',
12
     '/\sSimple Cache\s/' => ' <strong><a style="color:sienna; border-bottom: 1px dotted #000000;" href="http://www.beliefmedia.com/simple-php-cache" title="Download Simple Cache">Simple Cache</a></strong> '
13
   );
14
 
15
  /* Find and replace content */
16
  foreach ($replace as $pattern_search => $pattern_replace) {
17
    $content = preg_replace($pattern_search, $pattern_replace, $content);
18
  }
19
 
20
 return $content;
21
}
22
add_filter('the_content', 'beliefmedia_replace_custom_word');
23
add_filter('the_excerpt', 'beliefmedia_replace_custom_word');

If a regex isn't required, something like $content = str_replace(array_keys($replace), $replace, $content); will also perform the find and replace. We match a space around each character to avoid linking text that is already linked (you could also use a word boundary \b).

Find and Replace Words in a WordPress Post

The result of the find-and-replace on the first paragraph. Style the link as required.

In essence, the feature is providing a shortcode-style feature without having to use shortcode.

Considerations

  • I've always struggled with the backwards spelling of the USA and that of anyone else. You could write a post or article in your own locale and then translate it into the US equivalent. This doesn't work with our website because of words like 'color' that finds itself in HTML code... and we don't want replaced. So, you'll continue to see a mixed-bag from us.
  • If you use our geographic API, ensure you cache your result (to mitigate the risks of throttling). The majority of your visitors will come from a minority of people, so caching will speed up the process. If you expect high usage and you're not a client, use another service... it's likely you'll be periodically blocked.

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment

READY TO HAVE A CHAT? CALL US ANYTIME ON 1300 235 433