Skip to content

Instantly share code, notes, and snippets.

@paaljoachim
Last active October 9, 2024 00:32
Show Gist options
  • Save paaljoachim/8f6ec97d4317b4bf4719 to your computer and use it in GitHub Desktop.
Save paaljoachim/8f6ec97d4317b4bf4719 to your computer and use it in GitHub Desktop.
Adjusting the WordPress top admin toolbar
//
// Example 3. Adding a top level node I named Custom Made having it link to an external web site.
// Adding two nodes (sub menu links) Media Library and Plugins to the Custom Made menu.
//
// The following code adds a top level link and media library and plugins submenu links to the Custom Made drop down.
add_action( 'admin_bar_menu', 'add_nodes_to_admin_bar',999 );
function add_nodes_to_admin_bar($wp_admin_bar) {
// adds a top level parent node I called Custom Made
$wp_admin_bar->add_node( array(
'id' => 'custom',
'title' => 'Custom Made',
'href' => 'http://easywebdesigntutorials.com/', // Top level link Custom Made links to an external web site.
));
// adds the child node Media Library to Custom Made
$wp_admin_bar->add_node( array(
'parent' => 'custom',
'id' => 'media-libray', // If you reuse media library in two locations then the id name of the second link needs to be different.
'title' => 'Media Library',
'href' => esc_url( admin_url( 'upload.php' ) ),
'meta' => false
));
// adds the child node Plugins to Custom Made
$wp_admin_bar->add_node( array(
'parent' => 'custom',
'id' => 'plugins',
'title' => 'Plugins',
'href' => esc_url( admin_url( 'plugins.php' ) ),
'meta' => false
));
}
//
// Example 4. Removing all customizer links.
// Adding multiple nodes (sub menu links) Media Library, Plugins, Themes, All Pages, All Posts to the site name menu.
//
// Here is a top admin sub menu that I made to be included inside the site name menu.
add_action( 'admin_bar_menu', 'remove_customize',999 );
function remove_customize ($wp_admin_bar) {
// Removes all customizer links from the admin menu bar.
// Any of the top admin bar links can be removed in this way.
$wp_admin_bar->remove_node( 'customize' );
$wp_admin_bar->remove_node( 'customize-background' );
$wp_admin_bar->remove_node( 'customize-header' );
$wp_admin_bar->remove_node( 'customize-themes' );
$wp_admin_bar->remove_node( 'customize-widgets' );
}
add_action( 'admin_bar_menu', 'add_nodes_to_admin_bar',999 );
function add_nodes_to_admin_bar($wp_admin_bar) {
// Adds the child node Media Library to the site name parent node (submenu)
$wp_admin_bar->add_node( array(
'parent' => 'site-name',
'id' => 'media-libray', // NB! The id name can only be used once.
'title' => 'Media Library',
'href' => esc_url( admin_url( 'upload.php' ) ),
'meta' => false
));
// Adds Plugins.
$wp_admin_bar->add_node( array(
'parent' => 'site-name',
'id' => 'plugins',
'title' => 'Plugins',
'href' => esc_url( admin_url( 'plugins.php' ) ),
'meta' => false
));
// Adds Themes.
$wp_admin_bar->add_node( array(
'parent' => 'site-name',
'id' => 'themes',
'title' => 'Themes',
'href' => esc_url( admin_url( 'themes.php' ) ),
'meta' => false
));
// Adds All Pages.
$wp_admin_bar->add_node( array(
'parent' => 'site-name',
'id' => 'all-pages',
'title' => 'All Pages',
'href' => esc_url( admin_url( 'edit.php?post_type=page' ) ),
'meta' => false
));
// Adds All Posts.
$wp_admin_bar->add_node( array(
'parent' => 'site-name',
'id' => 'all-posts',
'title' => 'All Posts',
'href' => esc_url( admin_url( 'edit.php' ) ),
'meta' => false
));
}
// Example 5. Remove the admin toolbar.
//
// The following code removes the admin toolbar.
add_filter('show_admin_bar', '__return_false');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment