Last active
February 26, 2019 14:37
-
-
Save jehanf/b2b992c0e45aff76c77cc1acc2b422b0 to your computer and use it in GitHub Desktop.
Wordpress • Automatically adds No Follow / target='_blank" to all external links in post content / page content
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
<?php | |
class AddNoFollowExternalLinks { | |
public function __construct() { | |
add_filter( 'the_content', [$this, 'UrlParsing']); | |
} | |
public function GetLocalDomain() { | |
return $_SERVER['HTTP_HOST']; | |
} | |
public function IsInternalLink($url) { | |
$result = preg_match('/href(\s)*=(\s)*"[#|\/]*[a-zA-Z0-9-_\/]+"/', $url); | |
if ($result) { | |
return true; | |
} | |
$pos = strpos($url, $this->GetLocalDomain()); | |
if ($pos !== false) { | |
return true; | |
} | |
return false; | |
} | |
public function IsLinkExists($content = '') { | |
if ($content == '') { | |
return null; | |
} | |
$regex = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>"; | |
if(preg_match_all("/$regex/siU", $content, $links, PREG_SET_ORDER)) { | |
return $links; | |
} | |
return null; | |
} | |
public function AddTargetBlank($url, $tag) { | |
$noFollow = ''; | |
$pattern = '/target\s*=\s*"\s*_(blank|parent|self|top)\s*"/'; | |
if (preg_match($pattern, $url) === 0) { | |
$noFollow .= ' target="_blank"'; | |
} | |
if ($noFollow) { | |
$tag = $this->UpdateCloseTag($tag, $noFollow); | |
} | |
return $tag; | |
} | |
public function AddRelNofollow($url, $tag) { | |
$noFollow = ''; | |
$pattern = '/rel\s*=\s*\"[a-zA-Z0-9_\s]*\"/'; | |
$result = preg_match($pattern, $url, $match); | |
if ($result === 0) { | |
$noFollow .= ' rel="nofollow"'; | |
} else { | |
if (strpos($match[0], 'nofollow') === false && strpos($match[0], 'dofollow') === false) { | |
$temp = substr_replace($match[0], ' nofollow"', -1); | |
$tag = str_replace($match[0], $temp, $tag); | |
} | |
} | |
if ($noFollow) { | |
$tag = $this->UpdateCloseTag($tag, $noFollow); | |
} | |
return $tag; | |
} | |
public function UpdateCloseTag($tag, $noFollow) { | |
return substr_replace($tag, $noFollow . '>', -1); | |
} | |
public function UrlParsing( $content ) { | |
$matches = $this->IsLinkExists($content); | |
if ($matches === null) { | |
return $content; | |
} | |
// loop through each links | |
for ($i=0; $i < count($matches); $i++) | |
{ | |
$tag = $matches[$i][0]; | |
$url = $matches[$i][0]; | |
if(! $this->IsInternalLink($url)) { | |
$tag = $this->AddTargetBlank($url, $tag); | |
//exclude domain or add nofollow | |
$tag = $this->AddRelNofollow($url, $tag); | |
$content = str_replace($url, $tag, $content); | |
} | |
} // end for loop | |
$content = str_replace(']]>', ']]>', $content); | |
return $content; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just include the file or load it with your autoloader and do a simple
new AddNoFollowExternalLinks();