How to add a link to the WordPress admin bar with code

Updated on August 26, 2025

The WordPress Admin Toolbar sits at the top of your screen whenever you’re logged in, providing quick access to common dashboard functions. With just a small snippet of code, you can add your own link to the admin bar. In the example below, the link will appear directly in the toolbar and open in a new tab, so you can access it quickly without losing your place in WordPress.

Code

/**
 * Hook into the WordPress admin bar to add a copilot link.
 * This code can go inside your theme's functions.php
 * or in a custom plugin file.
 */

// Hook our function into the admin bar menu system.
// The "999" priority ensures our node is added after default ones.
add_action('admin_bar_menu', 'hx_add_copilot_help_admin_bar_link', 999);

/**
 * Callback function that adds a new item to the WordPress Admin Toolbar.
 *
 * @param WP_Admin_Bar $wp_admin_bar WordPress passes the global admin
 * bar object here.
 */
function hx_add_copilot_help_admin_bar_link($wp_admin_bar) {

    // Create an array of settings for our new admin bar node.
    // This tells WordPress what the menu item should look like and do.
    $args = array(
        // A unique ID for this node. Used internally by WP.
        // Important if you want to later remove or modify this node.
        'id' => 'copilot_help_admin_bar_link',

        // The text that will appear in the admin bar as the link label.
        'title' => 'Copilot',

        // The URL the link should go to when clicked.
        // Change this to your desired target.
        'href' => 'https://m365.cloud.microsoft/chat/',

        // "meta" defines extra attributes for the link.
        'meta' => array(
            // Adds a custom CSS class to the link (optional).
            'class' => 'copilot_help_admin_bar_link',

            // Tooltip text shown when you hover over the link.
            'title' => 'Go to https://m365.cloud.microsoft/chat/',

            // Forces the link to open in a new tab or window.
            'target' => '_blank',
        ),
    );

    // Use the WP_Admin_Bar object's method to add our new node.
    // Without this call, nothing will show in the toolbar.
    $wp_admin_bar->add_node($args);
}

How to use

First, change any of the values in the $args array to suit your needs, e.g., title, href, etc. If you are adapting this into a plugin, consider parameterizing the code.

Then, to install the code, we recommend a code plugin that manages custom snippets of code without modifying your theme files. The traditional way of adding custom code is to modify functions.php in your theme, but this approach is problematic because you will lose any changes with the next theme update. We prefer FluentSnippets as it is free and open source, but other popular options are Code Snippets and WPCode. Both have free versions that allow you to create custom shortcodes.

Reference

License

Licensed under CC BY 4.0

You are free to share and adapt this content for any purpose as long as you give appropriate credit in a reasonable manner.

No affiliate links

We do not participate in affiliate marketing, and we are not paid to mention products.

Leave a Reply

Your email address will not be published. Required fields are marked *