2

Auto-linking for "selected words" in the_content I am currently using the following code:

function wp_affiliate_links($text){
    $replace = array(

'/ example /' => ' <a rel="nofollow" href="http:example.com">example</a> ',
'/ Google /' => ' <a rel="nofollow" href="http:Google.com">Google</a> ',
    );
        }
    $text = preg_replace( array_keys($replace), $replace, $text, 1 );
    return $text;
    }
add_filter('the_content', 'wp_affiliate_links');

But the problem is that - it also try to replace the word that's already hyperlinked and spoils the whole code. If word "example" or Google are linked or comes into a previously linked phrase then it replace (and mess) inside the <a hreaf=""> tag too.

I want it to ignore few tags like <a>, <h1>, <button> etc. I've tried a lot but no luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.