Add link inside WP-Admin topbar

Marko Krstić
16 May 2021

Usually, some common settings are hidden under submenus and several clicks. To be efficient I need to have them in 1 or 2 clicks and highlighted. That’s why I made this small code snippet to add links and coloring for each menu item.

Single menu item

<?php
// Code to add singe link to the WordPress Toolbar
function dplugins_custom_toolbar_link($wp_admin_bar) {
    $args = array(
        'id' => 'oxy-template',
        'title' => 'Templates', 
        'href' => get_site_url() . '/wp-admin/edit.php?post_type=ct_template'
    );
    $wp_admin_bar->add_node($args);
}

// Code to add style in header for singe link to the WordPress Toolbar
function dplugins_custom_toolbar_link_color(){?>

<style>
  #wp-admin-bar-oxy-template .ab-item {
    background-color: #ffea00 !important; 
    color: black !important;
  }
</style>

<?php }
add_action('admin_bar_menu', 'dplugins_custom_toolbar_link', 999);
add_action('admin_head', 'dplugins_custom_toolbar_link_color');  
?>

Multiple menu items

<?php
// Code to add singe link to the WordPress Toolbar
function dplugins_custom_toolbar_link($wp_admin_bar) {
    // Link 1
    $args = array(
        'id' => 'oxy-template',
        'title' => 'Templates', 
        'href' => get_site_url() . '/wp-admin/edit.php?post_type=ct_template'
    );
    $wp_admin_bar->add_node($args);
    
    // Link 2
    $args = array(
        'id' => 'oxy-template2',
        'title' => 'Templates2', 
        'href' => get_site_url() . '/wp-admin/edit.php?post_type=ct_template'
    );
    $wp_admin_bar->add_node($args);
}

// Code to add style in header for singe link to the WordPress Toolbar
function dplugins_custom_toolbar_link_color(){?>

<style>
  #wp-admin-bar-oxy-template .ab-item {
    background-color: #ffea00 !important; 
    color: black !important;
  }
  
  #wp-admin-bar-oxy-template2 .ab-item {
    background-color: red !important; 
    color: black !important;
  }
</style>

<?php }
add_action('admin_bar_menu', 'dplugins_custom_toolbar_link', 999);
add_action('admin_head', 'dplugins_custom_toolbar_link_color');  
?>

Code Implementation

  1. Open Scripts Organizer.
  2. Code Blocks > Add New
screenshot 2021 05 13 at 14.55.20

Code Block Settings

  1. TRIGGER LOCATION > Admin only
  2. SCRIPT LOCATION > PHP

Was this article helpful?

Tags

Categories

Never miss new post again

Subscribe and get list of new posts in your inbox

Click to Copy