Home Blog Find and Replace Words in a WordPress Post

Find and Replace Words in a WordPress Post

Find Replace Wallpaper

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).

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

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.

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.


  • 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.

Shortt URL for this post: http://shor.tt/1XOl


Please enter your comment!
Please enter your name here