admin bar remove links from menu/sub-menu

PHOTO EMBED

Thu Sep 07 2023 18:40:09 GMT+0000 (Coordinated Universal Time)

Saved by @mastaklance

/**
 * Remove main link from admin bar - litespeed settings
 */

add_action('wp_before_admin_bar_render', 'disable_admin_bar_submenu_link');
function disable_admin_bar_submenu_link() {
    global $wp_admin_bar;

    // Parent menu item ID
    $parent_menu_id = 'wp-admin-bar-litespeed-menu'; // Replace with the ID of the parent menu item

    // Sub-menu item ID to disable or remove
    $submenu_item_id = 'wp-admin-bar-assetcleanup-parent'; // Replace with the ID of the sub-menu item

    // Get the parent menu item
    $parent_menu_item = $wp_admin_bar->get_node($parent_menu_id);

    // Check if the parent menu item exists
    if ($parent_menu_item) {
        // Get the sub-menu items
        $submenu_items = $parent_menu_item->get_children();

        // Loop through the sub-menu items
        foreach ($submenu_items as $submenu_item) {
            // Check if the sub-menu item matches the ID to disable or remove
            if ($submenu_item->id === $submenu_item_id) {
                // Disable or remove the sub-menu item
                // Uncomment one of the following lines based on your requirement

                // Disable the link
                // $submenu_item->href = 'javascript:void(0)';

                // Remove the sub-menu item completely
                // $parent_menu_item->remove_child($submenu_item_id);

                break; // Stop the loop
            }
        }

        // Update the parent menu item
        $wp_admin_bar->add_node($parent_menu_item);
    }
}


or v2
/**
 * Remove main link from admin bar - litespeed settings
 */
add_action('wp_before_admin_bar_render', 'disable_admin_bar_submenu_links');

function disable_admin_bar_submenu_links() {
    global $wp_admin_bar;

    // Parent menu item IDs to disable or remove
    $parent_menu_ids = array(
        'wp-admin-bar-litespeed-menu',   // Replace with the ID of the first parent menu item
        'wp-admin-bar-assetcleanup-parent',   // Replace with the ID of the second parent menu item
    );

    foreach ($parent_menu_ids as $parent_menu_id) {
        // Get the parent menu item
        $parent_menu_item = $wp_admin_bar->get_node($parent_menu_id);

        // Check if the parent menu item exists
        if ($parent_menu_item) {
            // Remove the sub-menu items completely
            $parent_menu_item->remove_children();

            // Update the parent menu item
            $wp_admin_bar->add_node($parent_menu_item);
        }
    }
}
content_copyCOPY