Skip to main content
WordPress made easy with the drag & drop Total WordPress Theme!Learn More

Automatically Replace Keywords With Affiliate Links In WordPress Posts

Last updated on:

For today’s daily trick I will show you a very simply method for replacing strings of text inĀ  your WordPress posts (keywords) with your affiliate links. To do so we’ll want to create a function that appends to your content hook and finds your keywords and replaces them with an HTML link.

Simply copy and paste the following in your functions.php file (make sure it’s within your PHP tags)

function keyword_link($keyword){
$replace = array(
    Themeforest' => '<a href="http://www.themeforest.net/ref=wpexplorer">ThemeForest</a>' // 'keyword' => 'transform into this link'
);
$keyword = str_replace(array_keys($replace), $replace, $keyword); return $keyword; }
add_filter('the_content', 'keyword_link');

Once you’ve added it to your functions.php you can replace the current keyword and link with your own or add more to the array by using comas to separate each listing in the array.

Limitation

The method above is a nice and quick way to do it which can be good for auto-pilot niche sites the problem is that it will transform any text with your keywords into links. This even includes text within your alt, rel and title tags. So basically if you have an image with an alt tag the same as your keyword from the array it will get f-ed up.

WordPress Search & Replace Plugin

If you need something more advanced that will allow you to replace any string of text with virtually anything you want you should check out the “Search and Replace For WordPress” plugin.

It’s a great way to add affiliate links to your blog but also to replace bad words on a community site to a PG version.

Search And Replace For WordPress

This plugin has an admin panel will you can set up a list of words or phrases and change them to whatever you want. Plus, you can also choose the different areas you want to search (post titles, content, excerpts, tags, comments…etc) as opposed to my method above which only searches “the_content”.

Search And Replace For WordPress Example

It’s quick easy and only $7. You can also see a snapshot of what the backend of the plugin looks like above. It’s a much faster way to find and replace than the manual method we first shared.

 

Subscribe to the Newsletter

Get our latest news, tutorials, guides, tips & deals delivered to your inbox.

2 Comments

  1. Waseem Senjer

    Hello,
    I think you might find my plugin interesting.
    I made a plugin called “Affiliate Butler Pro”. You can search it on CodeCanyon, it’s only $19.
    It doesn’t only convert keywords to links. But it also can convert them to internal links in the website. Moreover, it can convert the keywords to a popup which supports the following: (Customized HTML, Image, Gallery, Google Maps and Videos -Youtube and Vimeo). With a comprehensive stats about the hits on each keyword and which posts the hits came from.

    • AJ Clarke

      Cool looking plugin, thanks for sharing!

Leave a Reply

Your email address will not be published. Required fields are marked *

Learn how your comment data is processed by viewing our privacy policy here.