admin管理员组

文章数量:1122832

The standard code found all over the internet for replacing "Howdy" in the WordPress admin bar is now throwing a deprecated warning. This has worked for many years:

function replace_howdy( $wp_admin_bar ) {
    $my_account = $wp_admin_bar->get_node('my-account');
    $newtitle = str_replace( 'Howdy,', '', $my_account->title );            
    $wp_admin_bar->add_node( array(
        'id' => 'my-account',
        'title' => $newtitle,
    ) );
};
add_filter( 'admin_bar_menu', 'replace_howdy',25 );

Now I am getting a deprecated notice: Attempt to read property "title" on null in {the file} on {third line of this code}. Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated...

I assume that is because $my_account = $wp_admin_bar->get_node('my-account'); is either not identifying the correct node any longer, or the function is firing too early? Maybe my filter needs to target a different function?

How can I update and fix this?

When I try troubleshooting, this returns NULL:

global $wp_admin_bar;
$node_my_account = $wp_admin_bar->get_node('my-account');
var_dump($node_my_account); 

The standard code found all over the internet for replacing "Howdy" in the WordPress admin bar is now throwing a deprecated warning. This has worked for many years:

function replace_howdy( $wp_admin_bar ) {
    $my_account = $wp_admin_bar->get_node('my-account');
    $newtitle = str_replace( 'Howdy,', '', $my_account->title );            
    $wp_admin_bar->add_node( array(
        'id' => 'my-account',
        'title' => $newtitle,
    ) );
};
add_filter( 'admin_bar_menu', 'replace_howdy',25 );

Now I am getting a deprecated notice: Attempt to read property "title" on null in {the file} on {third line of this code}. Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated...

I assume that is because $my_account = $wp_admin_bar->get_node('my-account'); is either not identifying the correct node any longer, or the function is firing too early? Maybe my filter needs to target a different function?

How can I update and fix this?

When I try troubleshooting, this returns NULL:

global $wp_admin_bar;
$node_my_account = $wp_admin_bar->get_node('my-account');
var_dump($node_my_account); 
Share Improve this question edited Jul 30, 2024 at 15:41 hommealone asked Jul 29, 2024 at 23:03 hommealonehommealone 851 silver badge10 bronze badges 5
  • I don't have this text in the admin bar. do you have the last version of wordpress ? or maybe this text come from a plugin ? – mmm Commented Jul 30, 2024 at 0:15
  • The my-account node in the admin bar is, by default, the furthest item on the right. Since the early days of WordPress, it displays "Howdy, {username}" as anchor text of a link to the user's profile editing page. Upon upgrade to WP version 6.6 this function no longer works. – hommealone Commented Jul 30, 2024 at 14:29
  • oh sorry, I was in uk english and not in american, I see it now. the node you want to edit is on priority 9991 then you have to change the priority of the hook in your plugin : core.trac.wordpress.org/browser/tags/6.6.1/src/wp-includes/… – mmm Commented Jul 30, 2024 at 18:18
  • Is there an easy way to find the priority of admin bar nodes? – hommealone Commented Jul 30, 2024 at 22:50
  • By the way... besides throwing a deprecated warning, it simply no longer worked in WP version 6.6. Thanks, mmm. for pinpointing the reason. – hommealone Commented Jul 30, 2024 at 23:02
Add a comment  | 

1 Answer 1

Reset to default 4

Thank you @mmm that helps! Setting the priority to 9992 allows this function to work in WP V6.6

/** =============================================================
 * replace WordPress Howdy in Admin Bar with Hi in WordPress 6.6+
 * or remove howdy (replace with nothing)
 */
function replace_howdy( $wp_admin_bar ) {
    $my_account = $wp_admin_bar->get_node('my-account');
    if ( isset( $my_account->title ) ) {
        $newtitle = str_replace( 'Howdy,', '', $my_account->title );
        $wp_admin_bar->add_node( array(
            'id' => 'my-account',
            'title' => $newtitle,
        ) );
    }
};
add_filter( 'admin_bar_menu', 'replace_howdy', 9992 );

本文标签: admin barWordpress replace howdy function now throwing deprecated warning