Labs / WordPress Admin

Force “Visit Site” Admin Bar Links to Open in a New Tab

By default, WordPress opens the “Visit Site” link in the admin bar in the same browser tab. This replaces the current admin page and disrupts workflow when moving between the dashboard and the frontend.

This Lab provides a small, deterministic snippet that forces all frontend “Visit Site” admin bar links to open in a new browser tab, including multisite My Sites menu entries.

Scope

Installation

  1. Install the Code Snippets plugin or use a must-use plugin.
  2. Create a new snippet.
  3. Paste the code below. (remove <?php if using Code Snippets)
  4. Set the snippet to run in Admin only.
  5. Save and activate.

Code

<?php
/**
 * Force all "Visit Site" admin bar links to open in a new tab,
 * including Multisite "My Sites" menu entries.
 */
add_action( 'admin_bar_menu', function ( $wp_admin_bar ) {

	$nodes = $wp_admin_bar->get_nodes();

	if ( empty( $nodes ) ) {
		return;
	}

	foreach ( $nodes as $node ) {

		if ( empty( $node->href ) ) {
			continue;
		}

		// Skip admin URLs; only affect frontend site links.
		if ( strpos( $node->href, admin_url() ) === 0 ) {
			continue;
		}

		// Leave nodes that already define a target.
		if ( ! empty( $node->meta['target'] ) ) {
			continue;
		}

		$wp_admin_bar->add_node( [
			'id'     => $node->id,
			'title'  => $node->title,
			'href'   => $node->href,
			'parent' => $node->parent,
			'meta'   => array_merge(
				$node->meta ?? [],
				[
					'target' => '_blank',
					'rel'    => 'noopener noreferrer',
				]
			),
		] );
	}

}, 100 );

Notes

Status

This Lab is provided as a standalone snippet. It may later be folded into a plugin or remain a documented patch.