Insert nofollow sponsored to a dynamic title

  • Hello,

    I can’t find a way to insert rel “nofollow” and “sponsored” to a dynamic title that easily that link to a dynamic custom meta.

    For the image block and button block it’s quite easy, it’s there, visible and straight away.

    For the heading block, I can’t find it.

    Thanks for your help,
    Samuel

  • Hi Samuel,

    The Headline/Text block doesn’t currently expose a rel attribute field in the UI when using a dynamic link, unlike the Image and Button blocks. You can work around this using the render_block filter with a custom class applied to the block:

    add_filter( 'render_block', function( $block_content, $block ) {
        if ( ! is_admin() && ! empty( $block['attrs']['className'] ) ) {
            $classes = explode( ' ', $block['attrs']['className'] );
            $rel = [];
    
            if ( in_array( 'cu-add-nofollow', $classes ) ) {
                $rel[] = 'nofollow';
            }
    
            if ( in_array( 'cu-add-sponsored', $classes ) ) {
                $rel[] = 'sponsored';
            }
    
            if ( ! empty( $rel ) ) {
                $block_content = str_replace( '<a', '<a rel="' . implode( ' ', $rel ) . '"', $block_content );
            }
        }
        return $block_content;
    }, 10, 2 );

    Add cu-add-nofollow and/or cu-add-sponsored as additional CSS classes on the Text block. The filter will inject the appropriate rel attribute on the rendered anchor.

  • Ok, thanks for your help !

    This would be a good feature to add then, to make the whole dynamic process transparent in all ways.

  • Hello,

    Thank you, I have raised this as a feature request. Meanwhile, you can also suggest this in the feedback board.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.