Site logo

Archived topic

Add Rel=Sponsored / Nofollow to dynamic post meta links

3 replies · Started by Thomas on February 20, 2023

Viewing posts 1–4 of 4

We've recently created a new post template for product reviews, and we have 'buy now' style buttons that are populated using post meta data. The buttons are Headlines (which displays the relevant meta data) inside a container.

How can we set these as Rel=sponsored or nofollow, given there's no clear place to select this option?

Example review below.

Thanks,
Thomas

Hi there,

for now you need tom use some PHP.

1, Add this PHP Snippet to your site:


add_filter( 'render_block', function( $block_content, $block ) {
  if ( 
    !is_admin() 
    && ! empty( $block['attrs']['className'] ) 
  ) {
    if ( strpos( $block['attrs']['className'], 'nofollow' ) !== false ) {
      $block_content= str_replace( 'href','rel="nofollow" href' , $block_content );
    }
    if ( strpos( $block['attrs']['className'], 'sponsored' ) !== false ) {
      $block_content= str_replace( 'href','rel="sponsored" href' , $block_content );
    }

  }
  return $block_content;
}, 10, 2 );

Adding PHP: https://docs.generatepress.com/article/adding-php/

2. then select the block with the link and in Advanced > Additional CSS Class(es) add either: nofollow or sponsored

Works a charm! Many thanks @David.

Regards,
Thomas

Glad to hear that!

This archived topic is closed to new replies.