Skip to content

Instantly share code, notes, and snippets.

@erickarbe
Created March 28, 2017 15:06
Show Gist options
  • Save erickarbe/5ad0f24cc68f1061e46d506b2f25b4fe to your computer and use it in GitHub Desktop.
Save erickarbe/5ad0f24cc68f1061e46d506b2f25b4fe to your computer and use it in GitHub Desktop.
Add rel="nofollow" to only PDF links in Wordpress content and excerpt
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