Automatically Replace Words With Affiliate Links

function auto_add_affiliate_links($text){
    $replace_words = array(
        //text to search => text to replace
        'amazon' => '<a href="http://amazon.com/ref_id">Amazon</a>',
        'ebay' => '<a href="http://ebay.com/ref_id">Ebay</a>',
        'buy at amazon' => '<a href="http://amazon.com/ref_id">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');

Leave a Reply

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