Forked from JAW-Dev/WordPress - Nofollow for External Links in Comment Text.php
Created
May 30, 2016 16:49
-
-
Save jwthanh/5e0f74793bf29789b58f37cbc77777c4 to your computer and use it in GitHub Desktop.
Nofollow for external links in comment text
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
// source:http://www.onextrapixel.com/2012/10/12/5-code-snippets-for-interacting-with-wordpress-post-content-effectively/ | |
function auto_nofollow($content) { | |
//return stripslashes(wp_rel_nofollow($content)); | |
return preg_replace_callback('/<a>]+/', 'auto_nofollow_callback', $content); | |
} | |
add_filter('comment_text', 'auto_nofollow'); | |
function auto_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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment