• Basit

    (@basitbasit11333)


    Hi,
    I am using generatepress, I do blogging on it. You know when you are posting, outbound links becomes necessary.

    But when I was creating a outbound link and set it it “open in new tab”, generatepress automatically make that link “noreferer noopener”.

    Any code you can give me to set it as “noopener only” plz.

    Thank you

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @basitbasit11333,

    That’s controlled by WordPress. See: https://github.com/WordPress/gutenberg/issues/26914

    Perhaps you can try adding this snippet through a plugin like Code Snippets:

    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 );
    }
    Sourav Pan

    (@microbiologynote)

    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

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Link Query’ is closed to new replies.