[wp-trac] [WordPress Trac] #61738: admin_bar_menu node my-account returns null

WordPress Trac noreply at wordpress.org
Mon Jul 29 15:44:46 UTC 2024


#61738: admin_bar_menu node my-account returns null
--------------------------+---------------------
 Reporter:  michaelwp85   |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  6.6.2
Component:  Toolbar       |     Version:  6.6.1
 Severity:  normal        |  Resolution:
 Keywords:  has-patch     |     Focuses:
--------------------------+---------------------

Comment (by sabernhardt):

 I was not aware of `wp_before_admin_bar_render`, but that seems to filter
 existing node content well.

 To replace "Howdy" in the title, simply increasing the priority for the
 hook is a quick way to remove the error in 6.6.1 and to continue seeing a
 change in the visible text. However, the example is not thorough anymore,
 and it could be more defensive.
 1. The sub-menu recently added an ARIA label with the same text, using
 `menu_title` in the `meta` array.
 2. Switching the hook to `wp_before_admin_bar_render` (with the
 `$wp_admin_bar` global variable inside the function) can edit the text
 without defining a priority. My tests of that hook were successful with
 Core's `wp_admin_bar_my_account_item` priority at `7`, `9991` and
 `99999999`.
 3. The custom function could check at least whether the result of
 `get_node` is set before trying to edit part of the object. A similar
 [https://library.wpcode.com/snippet/r2mg3q2d/ snippet from WPCode] quits
 early if it does not find `$my_account->title`. Technically, another
 plugin (or some other code) might remove or replace the node that Core
 adds.

 {{{
 /**
  * Replaces the "Howdy" text in the WP admin toolbar.
  *
  * @global WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
  */
 function replace_howdy_in_admin_bar() {
         global $wp_admin_bar;

         $my_account = $wp_admin_bar->get_node( 'my-account' );

         // Return early if node contents are not available to edit.
         if ( ! isset( $my_account->title ) || ! isset(
 $my_account->meta['menu_title'] ) ) {
                 return;
         }

         $wp_admin_bar->add_node(
                 array(
                         'id'    => 'my-account',
                         'title' => str_replace( 'Howdy,', 'Hello,',
 $my_account->title ),
                         'meta'  => array(
                                 'menu_title' => str_replace( 'Howdy,',
 'Hello,', $my_account->meta['menu_title'] ),
                         ),
                 )
         );
 }
 add_action( 'wp_before_admin_bar_render', 'replace_howdy_in_admin_bar' );
 }}}

 Of course, the documentation needs updating:
 - The `wp_admin_bar_render` DocBlock says that `admin_bar_menu` adds menus
 at the "most optimal point, right before the admin bar is rendered." It
 may be the best hook for adding new menus and/or replacing them, but it
 does not fire //immediately// before rendering the admin bar.
 - The DocBlock for `wp_before_admin_bar_render` only says it "Fires before
 the admin bar is rendered." The additional information is at least
 available on the
 [https://developer.wordpress.org/reference/hooks/wp_before_admin_bar_render
 /#more-information DevHub page], though not in the DocBlock.
 - For manipulating existing nodes, or removing them,
 `wp_before_admin_bar_render` seems to be the better choice.

 My biggest concern with the error is that any visitor who has logged in
 can see it, but only site admins and/or plugin authors can do something to
 fix it.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/61738#comment:14>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list