Automatically Replace words by Affiliate Links

There are many paid plugins that can easily replace  the words by affiliate links but in this post I’d like to share a cool piece of code that can do the same thing for FREE.

It do not give you as much control as the plugins does, but in most cases this code should fit your needs.




Simply paste the code below into your functions.php file.

function replace_text_wps($text){
    $replace = array(
        // 'WORD TO REPLACE' => 'REPLACE WORD WITH THIS'
        'thesis' => '<a href="http://wpkube.com/go/thesis">thesis</a>',
        'studiopress' => '<a href="http://wpkube.com/go/ninja">ninja</a>'
    );
    $text = str_replace(array_keys($replace), $replace, $text);
    return $text;
}
add_filter('the_content', 'replace_text_wps');
add_filter('the_excerpt', 'replace_text_wps');

Don’t forget to enter your words & links as shown in the example code.

About the Author: Devesh Sharma is a WordPress fanatic and loves experimenting with WordPress themes & plugins. Get more from Devesh on  and Twitter.

You May Also Like:

29 comments… add one
  1. Great tip! Thank you so much for sharing it.

  2. Shiva Chettri says:

    This is really awesome piece of code Dev, I currently use the paid Ninja Affiliate to do the same thing….lol..never would have bought that if I knew I could do the same thing for free. The only negative point of using this code is that for every new affiliate you will need to edit the files. None the less thanks a lot

    • Yeah, one need to update the theme file to add a new affiliate link.

      Thanks for the comment shiva.

      Btw. I see you very active doing blog commenting. You’re doing great work man !!

  3. Heather C Stephens says:

    Hi Devesh!

    Fantastic tip! Thanks for sharing it. Will it work on any theme?

    Heather

  4. kevin Chard says:

    Hi Heather,
    My snippet should work for any theme without issues.
    Thanks for posting my snippet Devesh,

  5. Nice tip, but I guess this solution isn’t scalable and you have to modify the code every time some new replacement should be added. I recommend MBP Ninja Affiliate, most things are automated and you have better control.

  6. Great share, I normally don’t mess with too much code if I can avoid it. Or I just pass it off to a friend or outsource it but knowledge is power, my friends. Thanks for the resource.

    Jon

    • Glad you like it Jon. I can understand why you don’t want to mess with funtions.php file :) .

      Anyways, Thanks for the comment. Have a great weekend.

  7. Great tip! I edited my functions.php to show google ads to people who come to my site from search engines. I think those ppl have a higher chance of clicking an ad.

    If they decide to come back they see less ads the 2nd time around. Why punish the repeat reader with ads?

    • Hi Eric,

      Glad you like it too.

      That’s great Idea man. May be you can write a guest post about these, on wpkube.

      Thanks for the comment. Have a great weekend.

  8. Jayne Kopp says:

    Hi Devesh, you never cease to amaze me with your tidbits. As you know however, I’d likely come running to you to make these sorts of mods. Knowing me the results would be devastating! LOL.

    Thanks so much though for always being so helpful. I really appreciate you!

  9. Ian Belanger says:

    Hi Dev,

    Great tip, thanks for the code. I am currently learning how to edit WP sites in DW and this blog will really help me.

    Thanks for sharing such great info Dev!

    • That’s awesome to hear Ian.

      Thanks for the nice comment. Don’t hesitate to contact me if you need any help regarding wordpress.

  10. Jerome Ratliff says:

    HI Devesh,

    What a great tip. I didn’t even think of this. I wonder if it will work with the current theme I’m using today. Do you know if it matters or not?

    Jerome Ratliff

    • Hi Jerome,

      Glad you like it. It should work with every theme. Actually i just adding a function in functions.php file, so it doesn’t matter which theme/skin you’re using.

      Thanks. If you need any help then don’t hesitate to contact me.

  11. Dave Clements says:

    This is a great use of a simple code snippet that can eliminate yet another plugin. I haven’t ventured into affiliate marketing yet, but I think it might have to be in my future. How long have you been doing it? Do you think it’s worth its while?

    Thanks Devesh

    • Hi Dave,

      Yeah it’s great snippet.

      I’m in affiliate marketing from a year. Yeah it’s worth, I think affiliate marketing is one of the best way to make money online and it works awesome with every type of site.

      Actually I’m also running a blogging tips & mmo blog. You may want to check it out – Technshare.com :) .

      Thanks Dav. Have a great day.

      • Dave Clements says:

        Yeah, I’m familiar with Technshare :)

        I’m kind of reluctant to do the affiliate marketing thing. I keep putting it off, but I suppose I have more of a reason to than most because I get income from my blog by people hiring me as a WordPress consultant, so there’s less of an urge for me to make money from other avenues.

        Still, if it’s worth the effort, I might have to consider it one of these days.

        • That’s great to hear, Dave. I thought you don’t know about my other blog.

          It is good to see you’re generating good income by your skills but still i would suggest you to try affiliate marketing.

          If you ever need any help or advice regarding affiliate marketing then don’t hesitate to contact me.

          I would more then happy to help you anyway i can.

          ~Dev

  12. Vernessa Taylor says:

    Hi Devesh,

    Thanks for sharing this snippet (and thanks to Kevin). I’m no programmer but I like to use a little code instead of a plugin whenever I can.

    As a reminder to anyone who doesn’t want to muck around in the functions.php file (and who doesn’t have a techie like Devesh available), many modern themes come with an option to easily add your own custom code. There might be a special text box on the Options page or a Tab with an area to add it in. Code snippets like this one can be placed in those custom areas.

    • Hi Vernessa,

      You’re welcome, mate and thanks for taking time to comment here. I do appreciate it.

      Have a powerful day!

      ~Dev

  13. nice i m getting rid of my gocodes now :-D no unneccesary plugins ;-)

  14. Rosaria Lucchesi says:

    Very nice! Your piece of code can help me to build up an affiliate WordPress plugin.

Speak Your Mind...

A Weekly WordPress Newsletter...

Subscribe now and you will get -

»  Free Guide – Building a Successful WordPress Blog.
»  Weekly Newsletter curating useful blog posts and resources.
»  Exclusive Discount & Deals on Premium Products.

Your email will never be shared with anyone.