You can use the wp_targeted_link_rel() filter to change the link rel attribute for outbound links in GeneratePress. Here’s an example of how you can use this filter to set the link rel attribute to “noopener only” for outbound links:
function custom_link_rel( $rel, $link ) {
if ( strpos( $link, '//' ) === 0 && strpos( $link, home_url() ) === false ) {
$rel = 'noopener';
}
return $rel;
}
add_filter( 'wp_targeted_link_rel', 'custom_link_rel', 10, 2 );
You can add this code to your theme’s functions.php
file, or you can use a plugin like Code Snippets to add it.
The function first checks if the link is an outbound link using the strpos()
function, which checks if the link starts with “//” and doesn’t contain the home URL. If the link is an outbound link, the function changes the link rel attribute to “noopener”.
It’s important to note that this filter only affects links that are added to the post content, not the links that are added to the post meta or other places in the theme.
Also, “noopener” attribute is added to the link to prevent a newly opened tab from having access to the window.opener object, which can be used