These are my notes on the WordPress code snippet to open WordPress post format › Links in a new window.
Accessibility
It is understood that all pages should open in the same window.
It is not clear why accessibility businesses, consultants, experts, and websites are divided on this topic as those who agree do not follow their position with links opening in new windows.
With that said, I have a code snippet that allows me to insert a statement, (new tab) that will inform the user the link will open in a new tab.
Code snippet
add_filter( 'generateblocks_dynamic_url_output', function( $url, $attributes ) {
if ( ! empty( $attributes['className'] )
&& 'dynamic-title' === $attributes['className']
&& 'link' === get_post_format()
) {
$content = get_the_content();
$has_url = get_url_in_content( $content );
$url = ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
}
return $url;
}, 10, 2 );
add_filter( 'generate_get_the_title_parameters', function( $params ) {
if ( 'link' === get_post_format() ) {
$params = array(
'before' => sprintf(
'<h2 class="entry-title"%2$s><a target="_blank" href="%1$s" rel="bookmark nofollow noopener noreferrer">',
esc_url( generate_get_link_url() ),
'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : ''
),
'after' => '</a> (new tab) </h2>',
);
}
return $params;
} );
Known issues
- Code snippet works on post type, post
- Does not work on post type, page