- This topic has 8 replies, 3 voices, and was last updated 3 years, 4 months ago by
David.
-
AuthorPosts
-
December 16, 2022 at 1:05 am #2463761
Jodie
Hi,
I have a class of links on my website which are a different color.
Could I make that class automatically open in a new tab and be marked as “nofollow”?
Thanks for your help!
Cheers Jodie
December 16, 2022 at 1:08 am #2463765Fernando Customer Support
Hi Jodie,
Can you point us to a sample link that you want this attribute to be added to?
December 16, 2022 at 2:43 am #2463834Jodie
Hi Fernando,
Sure!
Cheers Jodie
December 16, 2022 at 3:59 am #2463921David
StaffCustomer SupportHi there,
try adding this PHP Snippet to the site:
add_filter( 'render_block', function( $block_content, $block ) { if ( ! empty( $block['attrs']['className'] ) && 'important-link' === $block['attrs']['className'] ) { $block_content = str_replace( '<a ', '<a target="_blank" rel="nofollow" ', $block_content ); } return $block_content; }, 10, 2 );December 16, 2022 at 6:03 pm #2464710Jodie
Hi David,
That worked perfectly, thank you so much!
How do you add notes in PHP so I can tell what function does which?
Thanks for your help.
Cheers Jodie
December 16, 2022 at 8:27 pm #2464761Jodie
Hi David,
Also, I have another Custom Class for a link I’d like to automatically open in a new tab.
How is best to do this? Thanks for your help!
Cheers Jodie
December 17, 2022 at 6:10 am #2465021David
StaffCustomer SupportComments in PHP – there are 2 types:
// single line commentsand
/* block of text across many line comments */Update code here with comments 🙂
// links with class important-link or goodreads-link // open in new window and rel no follow add_filter( 'render_block', function( $block_content, $block ) { // check the block for class names if ( ! empty( $block['attrs']['className'] ) && ( 'important-link' === $block['attrs']['className'] || 'goodreads-link' === $block['attrs']['className'] ) ) { $block_content = str_replace( '<a ', '<a target="_blank" rel="nofollow" ', $block_content ); } return $block_content; }, 10, 2 );December 17, 2022 at 7:08 pm #2465614Jodie
Hi Fernando,
Thank you so much! Much appreciated as always.
Cheers Jodie
December 18, 2022 at 5:09 am #2465860David
StaffCustomer SupportGlad we could be of help
-
AuthorPosts
- You must be logged in to reply to this topic.