Skip to content

Instantly share code, notes, and snippets.

@digisavvy
Created December 4, 2023 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digisavvy/3f7de0c97516dac4f0852e0dc6b857c8 to your computer and use it in GitHub Desktop.
Save digisavvy/3f7de0c97516dac4f0852e0dc6b857c8 to your computer and use it in GitHub Desktop.
<?php
/**
* Removes specified nodes from the WordPress admin bar.
*
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
*/
function remove_from_admin_bar( $wp_admin_bar ) {
// Conditional check to limit removal to non-admin area pages
if ( ! is_admin() ) {
// Remove 'comments' and 'editor_mode' nodes from the admin bar
$wp_admin_bar->remove_node( 'comments' );
$wp_admin_bar->remove_node( 'editor_mode' );
// Additional items can be removed by adding more remove_node calls here
// Example: $wp_admin_bar->remove_node( 'another-item-id' );
}
// Remove 'wp-logo' node from both the frontend and backend
$wp_admin_bar->remove_node( 'wp-logo' );
// Inline CSS to hide the label for the 'ws-form-node' menu item
echo '<style>li#wp-admin-bar-ws-form-node .ab-label { display: none; }</style>';
}
add_action( 'admin_bar_menu', 'remove_from_admin_bar', 999 );
/**
* Clears the title of specified admin bar nodes.
*
* This function iterates through all the nodes in the WordPress admin bar and
* clears the title of the nodes specified in the $clear_titles array. This is
* useful when the node is using a :before element for the icon and the text
* title is not needed.
*
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
*/
function clear_node_title( $wp_admin_bar ) {
// Get all the nodes from the admin bar
$all_toolbar_nodes = $wp_admin_bar->get_nodes();
// Specify the IDs of the nodes whose titles should be cleared
$clear_titles = array(
'site-name',
'customize',
);
// Iterate through all nodes to clear titles where needed
foreach ( $all_toolbar_nodes as $node ) {
// Check if the current node's ID is in the list to clear
if ( in_array( $node->id, $clear_titles, true ) ) {
// Copy the node's properties
$args = $node;
// Set the node title to an empty string
$args->title = '';
// Update the node in the admin bar
$wp_admin_bar->add_node( $args );
}
}
}
add_action( 'admin_bar_menu', 'clear_node_title', 999 );
/**
* Reorders the admin bar items according to a predefined sequence.
*
* This function reorders the admin bar items on the front-end to match a specified order.
* Items not included in the predefined list will be moved to the end of the admin bar.
*
* @global WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
*/
function reorder_admin_bar() {
global $wp_admin_bar;
if ( ! is_admin() ) {
// The desired order of identifiers (items)
$IDs_sequence = array(
'site-name',
'new-content',
'edit',
'bn-bricks',
'edit_with_bricks',
'ws-form-node',
'automatic-css-admin-bar',
'wp-rocket',
'redis-cache',
'seopress',
'query-monitor',
);
// Get an array of all the toolbar items on the current page
$nodes = $wp_admin_bar->get_nodes();
// Reorder nodes according to the specified $IDs_sequence
foreach ( $IDs_sequence as $id ) {
if ( isset( $nodes[ $id ] ) ) {
// Temporarily remove the menu item
$wp_admin_bar->remove_menu( $id );
// Add it back to place it at the end, effectively reordering it
$wp_admin_bar->add_node( $nodes[ $id ] );
// Remove the identifier from the list of nodes to avoid processing it again
unset( $nodes[ $id ] );
}
}
// Move any remaining nodes that were not specified in $IDs_sequence to the end
foreach ( $nodes as $id => $obj ) {
if ( empty( $obj->parent ) ) {
// Temporarily remove the menu item
$wp_admin_bar->remove_menu( $id );
// Add it back to place it at the end
$wp_admin_bar->add_node( $obj );
}
}
}
}
add_action( 'wp_before_admin_bar_render', 'reorder_admin_bar' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment