Block Editor social icons open in new tab and add hover titles

Last updated 3 months ago

Changes social links to target a new tab.

PHP
add_filter( 'render_block', function( $block_content, $block ) {
 
    // Social Links Only.
    if( $block['blockName'] != 'core/social-link' ) {
        return $block_content;
    }
 
    // Block Attributes
    // extract( $block['attrs'] );
    // $url, $service, $label
 
    // Change Labels To Titles.
    $block_content = str_replace(
        'aria-label=', 'title=', $block_content
    );
 
    // Add Target To Links.
    $block_content = str_replace(
        'href=',
        'target="_blank" rel="noreferrer noopener" href=',
        $block_content
    );
 
    // Output.
    return $block_content;
 
}, 10, 2 );

All code snippets are licensed GPLv2 or later unless otherwise stated.