WordPress Function to Replace Word with Affiliate Link

Let’s assume you have to add a specific link to the word “get regular updates” in any of your site or blog which might be your affiliate link. So every time you have to waste significant amount of time to replace the word with the link in WordPress. But you can repeat this task by applying a simple function in your WordPress theme. Here is the WordPress function to replace word with affiliate link. The link might be your affiliate link or any link you want. In our example we have replaced “get regular updates” by Get Regular Updates and the URL.



function replace_text_wps($text){
$replace = array(
‘check my doc’ => ‘<a href=”http://mysite.com/myafflink”>check my doc</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’);


Here is the front end content where the word is replaced by the link:

Do you think it is useful then please leave your comment below and get more WordPress query and function in WordPress Query category.

Author: mytutorialguru

Share This Post On

Submit a Comment

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