• I need a code for my functions.php theme that all my external lnks matching /go/ to have the atribute rel=”sponored”.

    I tried this code, by relacing the word nofollow but is not working. I use enfold theme, and pages, not blog posts, al least now. Is there a code that can replace in widgets, footer, pages, posts, basicly everywhere in the page where is an external link?

    Also it should check if already has that atribute.

    /*
     *  NOFOLLOW EXTERNAL LINKS
     *************************************************************/
    add_filter('the_content', 'my_nofollow');
    add_filter('the_excerpt', 'my_nofollow');
    function my_nofollow($content) {
        return preg_replace_callback('/<a[^>]+/', 'my_nofollow_callback', $content);
    }
    function my_nofollow_callback($matches) {
        $link = $matches[0];
        $site_link = get_bloginfo('url');
        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)) {
            $link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $link);
        }
        return $link;
    }
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    All the various page elements would utilize a different filter, if there is a filter available at all. Availability in some cases is theme or plugin dependent. There is not a universal solution for this in PHP. I think you’d have more success doing so through JavaScript or jQuery after the page loads into the browser. The problem with a JavaScript approach is search bots often do not execute JavaScript, so they wouldn’t “see” the added attribute.

Viewing 1 replies (of 1 total)
  • The topic ‘Rel=sponsored in all pages for external links, functions.php’ is closed to new replies.