Created
March 28, 2017 15:06
-
-
Save erickarbe/5ad0f24cc68f1061e46d506b2f25b4fe to your computer and use it in GitHub Desktop.
Add rel="nofollow" to only PDF links in Wordpress content and excerpt
This file contains hidden or 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_filter('the_content', 'my_nofollow'); | |
add_filter('the_excerpt', 'my_nofollow'); | |
function my_nofollow($content) { | |
return preg_replace_callback('/<a[^>]+\\.pdf/', 'my_nofollow_callback', $content); | |
} | |
function my_nofollow_callback($matches) { | |
$link = $matches[0]; | |
$link = preg_replace("%(href=\S(?!$link))%i", 'rel="nofollow" $1', $link); | |
return $link; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment