Search code examples
phpwordpresshrefadmintarget

add target="_blank" to a link using WP_Admin_Bar::add_node() in Wordpress


I coded this custom widget for the admin-bar of a Wordpress install i'm working on:

function custom_admin_bar_link_1($wp_admin_bar) {
$args = array(
    'id' => 'cal-cloudflare',
    'title' => 'Cloudflare', 
    'href' => 'https://dash.cloudflare.com/example.com/caching/configuration',
    'meta' => array(
        'class' => 'cal-cloudflare', 
        'title' => 'Clear Cloudflare Cache'
        )
);
$wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'custom_admin_bar_link_1', 999);

It works fine, but i want the link to open in a new windows when clicked. I tried this, but to no avail:

function custom_admin_bar_link_1($wp_admin_bar) {

$args = array(
    'id' => 'cal-cloudflare',
    'title' => 'Cloudflare', 
    'meta' => array(
        'class' => 'cal-cloudflare', 
        'title' => 'Clear Cloudflare Cache'
        ),
    'a' => array(
        'href' => 'https://dash.cloudflare.com/example.com/caching/configuration', 
        'target' => '_blank'
        )
);
$wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'custom_admin_bar_link_1', 999);

What needs to be done here?

Thanks.


Solution

  • Use the following instead:

    function custom_admin_bar_link_1($wp_admin_bar) {
        $args = array(
            'id'    => 'cal-cloudflare',
            'title' => 'Cloudflare', 
            'href'  => 'https://dash.cloudflare.com/example.com/caching/configuration', 
            'meta'  => array(
                'class'  => 'cal-cloudflare', 
                'title'  => 'Clear Cloudflare Cache',
                'target' => '_blank',
            ),
        );
        $wp_admin_bar->add_node($args);
    }
    add_action('admin_bar_menu', 'custom_admin_bar_link_1', 999);
    

    Tested and works.

    See: WP_Admin_Bar::add_node() method parameters