Hello guys i am using following code to make all external links nofollow.

add_filter('the_content', 'my_nofollow');
add_filter('the_excerpt', 'my_nofollow');

function my_nofollow($content) {
return preg_replace_callback('/<a[^>]+/', 'my_nofollow_callback', $content);
function my_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;

But after adding this code in my site's Theme Functions all links are still dofollow.

I am using Advanced Custom Fields Pro plugin and links adding by using this plugin are still dofollow.

How to make all external links no follow?

1 Answer 1


Edit 2: By suggest of @Mark Kaplun, the general solution should be this. We will do filtering after the page is generated, so we won't care about which plugin we're using.

We need a hack here to get whole page:


add_action('shutdown', function() {
    $final = '';

    // We'll need to get the number of ob levels we're in, so that we can iterate over each, collecting
    // that buffer's output into the final output.
    $levels = ob_get_level();

    for ($i = 0; $i < $levels; $i++)
        $final .= ob_get_clean();

    // Apply any filters to the final output
    echo apply_filters('final_output', $final);
}, 0);

I take this code from this question

Then you can remove 2 old filter:

add_filter('the_content', 'my_nofollow');
add_filter('the_excerpt', 'my_nofollow');

Use only one instead:

add_filter('final_output', 'my_nofollow');

Old answer

Just add one more filter.

add_filter('acf/load_value/name=my_field', 'my_nofollow' );

Edit: The concept to solve the problem here is filter the content that generated by plugin, for you here is ACF. Because the_content is from WP, not the plugin you're using, so add filter to the_content won't help.

  • and one filter there, and another one just there, and there is one more cumming from a third plugin. This is a solution if he wanted only for it to be done for a specific area, but it fails as a general solution Commented Apr 17, 2016 at 19:13
  • I think he's doing good with the normal case, he got problem with a plugin (ACF here) and his question about dealing with that plugin. I try to solve hist problem fist. I will edit my answer to be more general. Thank @MarkKaplun. I will notice that in future.
    – Tung Du
    Commented Apr 17, 2016 at 19:18
  • actually, the right solution is to filter the page after it is generated ;) Then you don't care what plugins are there and which one is active Commented Apr 17, 2016 at 19:40
  • Got this. I will edit my answer again :).
    – Tung Du
    Commented Apr 17, 2016 at 20:20
  • @MarkKaplun Thanks for pointing me out, very appreciate.
    – Tung Du
    Commented Apr 17, 2016 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.