admin管理员组

文章数量:1122846

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?

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?

Share Improve this question asked Mar 12, 2019 at 23:57 Timmy D.Timmy D. 336 bronze badges 6
  • Why do you want the noreferrer removed out of curiosity? – Tom J Nowell Commented Mar 13, 2019 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 Commented Mar 13, 2019 at 1:36
  • As an aside, did you open an issue on the gutenberg github about this? – Tom J Nowell Commented Mar 13, 2019 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. Commented Mar 13, 2019 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ż Commented Mar 13, 2019 at 4:35
 |  Show 1 more comment

1 Answer 1

Reset to default 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.

本文标签: Is there a hook or filter yet for Gutenberg Block Editor to not autoadd Noreferrer to links with a target