RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.38% (6.14%*) • Home Loan Fixed: 5.44% (6.26%*) • Fixed: 5.44% (6.26%*) • Variable: 5.38% (6.14%*) • Investment IO: 5.69% (6.52%*) • Investment PI: 5.49% (5.98%*)

Find and Replace Words in a WordPress Post

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

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
function beliefmedia_change_text($content) {
8
 
9
   /* Example code to get state */
10
   $state = beliefmedia_geo_state();
11
 
12
   /* Array for each preferred location */
13
   switch ($state) {
14
 
15
       case 'New South Wales':
16
           $patterns = array(
17
             '/%%name%%/' => 'Marty',
18
             '/%%city%%/' => 'Sydney',
19
           );
20
           break;
21
 
22
       case 'Queensland':
23
           $patterns = array(
24
             '/%%name%%/' => 'NotMarty',
25
             '/%%city%%/' => 'Brisbane',
26
           );
27
           break;
28
 
29
       case 'Victoria':
30
           $patterns = array(
31
             '/%%name%%/' => 'SomeStooge',
32
             '/%%city%%/' => 'Melbourne',
33
           );
34
           break;
35
 
36
       case 'Unknown':
37
           $patterns = array(
38
             '/%%name%%/' => 'Martin',
39
             '/%%city%%/' => 'Sydney',
40
           );
41
           break;
42
 
43
       default:
44
           $patterns = array(
45
             '/%%name%%/' => 'Martin',
46
             '/%%city%%/' => 'Sydney',
47
           );
48
           break;
49
   }
50
 
51
   /* Search and destroy */
52
   foreach ($patterns as $pattern_search => $pattern_replace) {
53
     $content = preg_replace($pattern_search, $pattern_replace, $content);
54
   }
55
 
56
 return $content;
57
}
58
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
function beliefmedia_replace_custom_word($content){
8
   $replace = array(
9
     '/\sWordPress\s/' => ' <a style="color:sienna; border-bottom: 1px dotted #E67300;" href="http://www.beliefmedia.com/" title="BeliefMedia">WordPress</a> ',
10
     '/\smarketing\s/' => ' <a style="color:sienna; border-bottom: 1px dotted #E67300;" href="http://www.martinkhoury.com/" title="MartyKhoury">marketing</a> ',
11
     '/\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> '
12
   );
13
 
14
  /* Find and replace content */
15
  foreach ($replace as $pattern_search => $pattern_replace) {
16
    $content = preg_replace($pattern_search, $pattern_replace, $content);
17
  }
18
 
19
 return $content;
20
}
21
add_filter('the_content', 'beliefmedia_replace_custom_word');
22
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.

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment