Menu Close

Automatically Replace Keywords With Affiliate Links In WordPress Posts

default

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.

 

View Source
Posted in WordPress