0

Wordpress adds "noreferrer" to all links that open in a new tab. This started before Gutenberg and a TinyMCE filter was provided to fix it. This no longer works for Gutenberg. They have a new wp_targeted_link_rel filter too that only works for the classic editor.

I manually removed it from all my links and all was fine on WP 5.0. Upon updating to WP 5.1 it was added back to all of my links after updating the posts. This is a nightmare.

The reason to remove it has to do with tracking the originating domain with affiliate networks. Even those that use affiliate ID's as URL parameters still demand that the referrer be an approved domain.

Has anyone found a way to stop this on Gutenberg yet?

  • Why do you want the noreferrer removed out of curiosity? – Tom J Nowell Mar 13 at 1:29
  • It looks like this happens in createLinkFormat in javascript, the solution here is almost certainly going to either be a JS one, or something that runs on the_content, but I don't see why you would want to remove it, some context on your question as to why would be super helpful and better inform any answers you might get – Tom J Nowell Mar 13 at 1:36
  • As an aside, did you open an issue on the gutenberg github about this? – Tom J Nowell Mar 13 at 1:37
  • 2
    @TomJNowell I said why it needs to be removed in the post. They removed it before because it's a necessity but it snuck back in with Gutenberg. I have not opened an issue on github. A JS solution could work but I'd rather it be gone in the database. I saw a wp_remove_targeted_link_rel_filters() was added in 5.1 but I can't get that to work. It might just be for the classic editor. I could just do a preg_replace filter on the_content but that seems awfully wasteful. – Timmy D. Mar 13 at 3:02
  • 1
    @TomJNowell why github? Isn’t the trac the official way to report bugs any more? All this duality I starting to get really annoying... – Krzysiek Dróżdż Mar 13 at 4:35
0

Just for others that see this post, as I am sure you are already aware, the noreferrer is added as a security fix for IE and Edge browsers and a few others, that do not support the rel="noopener" security fix for links that are opened in a new tab.

To remove the noreferrer, it is still a preg_replace. I added this to functions.php (backing it up first).

//This code removes noreferrer from your new or updated posts
add_filter( 'wp_targeted_link_rel', 'my_targeted_link_rel_remove_noreferrer');
function my_targeted_link_rel_remove_noreferrer( $rel_values ) {
return preg_replace( '/noreferrer\s*/i', '', $rel_values );
}

The first time I was testing, I had to add a priority to the filter call (below code) to make it work, not sure that "999" was really necessary, but now when I test it seems to work without it.

//This code removes noreferrer from your new or updated posts
add_filter( 'wp_targeted_link_rel', 'my_targeted_link_rel_remove_noreferrer', 999);
function my_targeted_link_rel_remove_noreferrer( $rel_values ) {
return preg_replace( '/noreferrer\s*/i', '', $rel_values );
}

Here is the link to the full post. https://wpbloggerassist.com/remove-noreferrer-in-gutenberg-from-the-latest-wordpress-update/

Editing to add I found another code here:

// from: https://tinygod.pt/gutenberg-adds-noopener-noreferrer-to-links/ function my_links_control( $rel, $link ) { return false; } add_filter( 'wp_targeted_link_rel', 'my_links_control', 10, 2 );

NOTE: This does not remove existing noreferrer links, you still need to go to posts, remove the "noreferrer" text and update.

This third code will not auto-add any rel attributes. So it is up to you to make sure to add the rel="noopener" manually.

  • Hi @WP Blogger Assist, I tried this before posting this question, having seen your blog post prior. I could not get that filter wp_targeted_link_rel to do anything regardless of the priority. I saw you had a the_content filter there too, which a friend of mine reports works but doesn't seem to be very efficient. I think Tom J Nowell is correct in saying we probably need to settle for a javascript option for now. – Timmy D. Mar 13 at 22:40
  • @Timmy D. The fix was working on my test site with Gutenberg before it auto-updated to 5.1.1. Looks like I'll have to test some more. But the strtr, you can combine several items, comma separated into the one call, I also remove the empty p tags that Wordpress adds like this $replace = array("noreferrer" => "", "<p></p>" => "" ); – WP Blogger Assist Mar 16 at 22:03
  • @Timmy D. Sorry I switched themes on my Gutenberg test site and that is why the change wasn't working for me just now. After you update your functions.php, you need to go back to your posts and manually delete the "noreferrer" from the links, and update the post again. It should work right away for any new posts. – WP Blogger Assist Mar 16 at 22:35
  • I hate to report back to you but neither is working, even with the priority, which was the case when I first saw your blog post. I simply can't get it to work, with new posts or old posts on the Block Editor (haven't tried classic editor since it's not relevant for me at this point). – Timmy D. Mar 18 at 21:18
  • @Timmy D, just updated with a new code I found, not sure if you already tried it or if you found a solution in the mean time. If that doesn't help, could you share the theme you are using - if it is a child theme, wordpress version and php version. I know there is also an open call for the wp_targeted_link_rel not working as expected in some situations on the Wordpress core, I am following that thread now too. – WP Blogger Assist May 24 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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