Recipe: add an item to the admin bar next to Addofy
Goal: your plugin adds an item to the admin bar and you control where it sits relative to Addofy's search, notices and account menu.
Add your own node
Your own nodes go through the native admin_bar_menu action. Addofy adds its pieces at fixed priorities, so you choose a side:
- Priority 1: Addofy adds the sidebar toggle.
- Priority 100: Addofy adds its nodes (search, notices and the account menu items).
PHP_INT_MAX: Addofy tidies the account menu, with Log Out last.
Hook below 100 to place your node before Addofy's nodes, above 100 to place it after.
add_action( 'admin_bar_menu', static function ( WP_Admin_Bar $bar ) {
$bar->add_node( array(
'id' => 'myplugin-status',
'title' => esc_html__( 'Status', 'myplugin' ),
'href' => admin_url( 'admin.php?page=myplugin' ),
) );
}, 120 ); // After the Addofy nodes.
Change Addofy's own nodes
adba_admin_bar_nodes filters the nodes Addofy adds. Add, reorder or remove them there. Each node is an argument array for WP_Admin_Bar::add_node(), in order, and the filter also receives the current screen id.
add_filter( 'adba_admin_bar_nodes', static function ( array $nodes, string $screen_id ) {
return $nodes; // Return the list, changed or not.
}, 10, 2 );
Nodes of WordPress and other plugins keep going through admin_bar_menu. Addofy only filters its own.
Related
- Writing an Addofy module: the timing table.
- Shell, admin bar and preferences hooks:
adba_admin_bar_nodes,adba_account_menu_itemsandadba_mobile_adminbar_keep.