Rel=sponsored in all pages for external links, functions.php
-
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)
Viewing 1 replies (of 1 total)
- The topic ‘Rel=sponsored in all pages for external links, functions.php’ is closed to new replies.