0

I would like to know how I can set all external links in all posts from a certain wordpress user in my blog automatically to "nofollow" ? So that old postings and future postings from this user are affected by this "nofollow" command. How is that possible?

Thank you so much!

1

You want to try something like this (untested):

// Nofollow in content
$author_id = get_the_author_meta( 'ID' );
add_filter('the_content', 'my_nofollow');

function my_nofollow($content) {
    //return stripslashes(wp_rel_nofollow($content));

    return preg_replace_callback('/<a[^>]+/', 'my_nofollow_callback', $content);
}

function my_nofollow_callback($matches, $author_id) {
    $link = $matches[0];
    $site_link = get_bloginfo('url');
    if ($author_id === 4) {
        if (strpos($link, 'rel') === false) {
            $link = preg_replace("%(href=\S(?!$site_link))%i", 'rel="nofollow" $1', $link);
        } elseif (preg_match("%href=\S(?!$site_link)%i", $link) && (strpos($link, 'rel') === false)) {
            $link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $link);
        }
    }
return $link;
}

Note: This is based on a nofollow function I am using on one of my sites and I just added a conditional for if ( is_author () ) in there

| improve this answer | |
  • Wow, nice! You have to know that I am a coding noob. So please can you tell me where I have to add this code? I guess in the function file? And: where you wrote the '4' I have to write the authors name right? THX a bunch :) – Patrick Dreid Feb 16 at 9:43
  • Yes, you write the Author name in there or the Author ID. It goes in functions.php anywhere eg at the bottom. If there are any errors let me know. – TomC Feb 16 at 10:18
  • Unfortunately it does not work. I tried the author id and the name but there is no nofollow in the authors postings. if (is_author('4')) { AND if (is_author('AUTHORNAME')) { does not work – Patrick Dreid Feb 16 at 14:24
  • I've modified my answer. Where I've used 4 as the author, you need to change this to the actual ID of your author. The old version used is_author which is deprecated and also I shouldn't have put it inside apostrophes. – TomC Feb 16 at 18:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.