in WordPress

Automatically Replace Words With Affiliate Links

Insert Link Using Wordpress it allows you to add bits of extra code to change the framework by using Wordpress hooks. Some of the most popular places to add hooks is before or after the content so you can add related ads to the content, but I'm going to show you how you can automatically change the content to add affiliate links. ## Affiliate Programs

Do you use affiliate programs on your blog? Affiliate programs allow you to direct your visitors at someone else's product and for each person you direct to the product you will get a percentage of the sale. Affiliate programs are a great way for you to earn extra passive income from your blog posts. The problem with this is that if you are talking about a certain product you have to remember to link to the product with your affiliate reference. What if you forget to add your affiliate reference? Then you won't get paid for advertising the product so you have wasted the chance for income. ## Automatically Replace Words With Affiliate Links Wordpress

In Wordpress we can access the content text before it is published this is an ideal place to check the content for places you can link to affiliate products. Using the content hook you can setup an array of words you want to check against and Wordpress can automatically replace these with affiliate links. Below is the code snippet to add the affiliate links just put this code in your functions.php file.

function auto_add_affiliate_links($text){
    $replace_words = array(
        //text to search => text to replace
        'amazon' => '<a href="">Amazon</a>',
        'ebay' => '<a href="">Ebay</a>',
        'buy at amazon' => '<a href="">buy at amazon</a>'
    $text = str_replace(array_keys($replace_words), $replace_words, $text);
    return $text;

add_filter('the_content', 'auto_add_affiliate_links');
add_filter('the_excerpt', 'auto_add_affiliate_links');

As you can see from the code you can search and replace on a keyword or a keyword phrase. In the example you set the key of the array to the word you are searching for and the value will be what it is replaced with. The best thing about this code snippet is that it will even add affiliate links to articles you have already written, so if you spoke about a product last year adding this snippet will replace the keyword with the affiliate link on old articles.

Upgrade to access all content on Paulund

Members unlock all tutorials and snippets

Access to all downloadable content

Access to code examples before others

Sign Up Now

Already a member? Login here

Subscribe To The Weekly Newsletter

Get weekly updates to your email