Created
April 12, 2017 02:10
-
-
Save Kaiderella/852fb93d528b8f0ea69aa9fc6a8f4243 to your computer and use it in GitHub Desktop.
Nofollow tất cả external links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//* Add rel nofollow to all external links | |
function wp_nofollow($content) | |
{ | |
return preg_replace_callback('/<a[^>]+/', 'wp_nofollow_callback', $content); | |
} | |
function wp_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; | |
} | |
add_filter('the_content', 'wp_nofollow'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment