Automatically adds rel=”nofollow” to external links.
function dr_external_links_nofollow( $content ) {
return preg_replace_callback(
'/<a[^>]+href=["'](http[^"']+)["'][^>]*>/i',
function( $matches ) {
$host = parse_url( home_url(), PHP_URL_HOST );
if ( strpos( $matches[1], $host ) === false ) {
return str_replace( '<a ', '<a rel="nofollow" ', $matches[0] );
}
return $matches[0];
},
$content
);
}
add_filter( 'the_content', 'dr_external_links_nofollow' );