How to Change Order of "Forms" Link in WordPress Toolbar [RESOLVED]

The code snippet provided below allows users to change the order of their WordPress Admin Bar (i.e., toolbar) menu items. The one listed below orders our toolbar menu items in alphabetical order from left to right.

It works perfect for all of our items except Gravity Forms. We noticed Gravity Forms is not coded 100% to WordPress standards, so that may be the reason why. We are using the proper Node ID (i.e., ‘gform-forms’) assigned by Gravity Forms, but it is not working.

This is how we identifed the NODE ID: Screenshot by Lightshot
This is what we get after applying the code snippet: Screenshot by Lightshot

Further, we are unable to remove the Gravity Forms menu item from our toolbar using the second code snippet provided below. It works well with all other menu items.

Bottom line, we need a fix for this unique Gravity Forms issue.

Thank you!


Code Snippet to Reorder WordPress Admin Bar (Toolbar) Menu Items

function reorder_admin_bar() {
    global $wp_admin_bar;

    // The desired order of identifiers (items)
    $IDs_sequence = array(
        'wp-logo',
        'site-name',
        'new-content',
		'comments',
		'analytify',
		'gform-forms',
		'gappointments',
		'perfmatters',
		'SG_CachePress_Supercacher_Purge',
		'rank-math',
		'tribe-events'
    );

    // Get an array of all the toolbar items on the current
    // page
    $nodes = $wp_admin_bar->get_nodes();

    // Perform recognized identifiers
    foreach ( $IDs_sequence as $id ) {
        if ( ! isset($nodes[$id]) ) continue;

        // This will cause the identifier to act as the last
        // menu item
        $wp_admin_bar->remove_menu($id);
        $wp_admin_bar->add_node($nodes[$id]);

        // Remove the identifier from the list of nodes
        unset($nodes[$id]);
    }

    // Unknown identifiers will be moved to appear after known
    // identifiers
    foreach ( $nodes as $id => &$obj ) {
        // There is no need to organize unknown children
        // identifiers (sub items)
        if ( ! empty($obj->parent) ) continue;

        // This will cause the identifier to act as the last
        // menu item
        $wp_admin_bar->remove_menu($id);
        $wp_admin_bar->add_node($obj);
    }
}
add_action( 'wp_before_admin_bar_render', 'reorder_admin_bar');

Code Snippet to Delete WordPress Admin Bar (Toolbar) Menu Item

function remove_link_from_admin_bar( $wp_admin_bar ) {
	$wp_admin_bar->remove_node( 'gform-forms' );
}

add_action( 'admin_bar_menu', 'remove_link_from_admin_bar', 999 );

Solution:

After further research and code changes, the code snippets provided below solved our issues.

Cheers!


Code Snippet to Reorder WordPress Admin Bar (Toolbar) Menu Items

function reorder_admin_bar() {
    global $wp_admin_bar;

    // The desired order of identifiers (items)
    $IDs_sequence = array(
        'wp-logo',
        'site-name',
        'new-content',
		'comments',
		'analytify',
		'tribe-events',
		'gform-forms',
		'gappointments',
		'perfmatters',
		'SG_CachePress_Supercacher_Purge',
		'rank-math'
    );

    // Get an array of all the toolbar items on the current page
    $nodes = $wp_admin_bar->get_nodes();

    // Perform recognized identifiers
    foreach ( $IDs_sequence as $id ) {
        if ( ! isset($nodes[$id]) ) continue;

        // This will cause the identifier to act as the last menu item
        $wp_admin_bar->remove_menu($id);
        $wp_admin_bar->add_node($nodes[$id]);

        // Remove the identifier from the list of nodes
        unset($nodes[$id]);
    }

    // Unknown identifiers will be moved to appear after known identifiers
    foreach ( $nodes as $id => &$obj ) {
        
		// There is no need to organize unknown children identifiers (sub items)
        if ( ! empty($obj->parent) ) continue;

        // This will cause the identifier to act as the last menu item
        $wp_admin_bar->remove_menu($id);
        $wp_admin_bar->add_node($obj);
    }
}
add_action( 'wp_before_admin_bar_render', 'reorder_admin_bar', 999);

Code Snippet to Delete WordPress Admin Bar (Toolbar) Menu Item

// Use this code snippet for plugins that do not follow WordPress' preferred coding standards (i.e., use the "wp_before_admin_bar_render" hook)
function remove_toolbar_menu_items() {
	
	global $wp_admin_bar;
	
	// use the menu item node id
	$wp_admin_bar->remove_menu('gform-forms');
}
add_action('wp_before_admin_bar_render', 'remove_toolbar_menu_items', 999);

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.