Skip to content

Instantly share code, notes, and snippets.

@solagirl
Created August 13, 2021 11:42
Show Gist options
  • Select an option

  • Save solagirl/0b35d19d653e9b80cd9c95fc34b94426 to your computer and use it in GitHub Desktop.

Select an option

Save solagirl/0b35d19d653e9b80cd9c95fc34b94426 to your computer and use it in GitHub Desktop.
Remove avatar image from WordPress admin bar
add_action( 'add_admin_bar_menus', function(){
	global $wp_admin_bar;
	if ( ! is_admin_bar_showing() || ! is_object( $wp_admin_bar ) ) {
		return;
	}
	remove_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
	add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item_no_avatar', 7 );
});

function wp_admin_bar_my_account_item_no_avatar( $wp_admin_bar ) {
	$user_id      = get_current_user_id();
	$current_user = wp_get_current_user();

	if ( ! $user_id ) {
		return;
	}

	if ( current_user_can( 'read' ) ) {
		$profile_url = get_edit_profile_url( $user_id );
	} elseif ( is_multisite() ) {
		$profile_url = get_dashboard_url( $user_id, 'profile.php' );
	} else {
		$profile_url = false;
	}

	/* translators: %s: Current user's display name. */
	$howdy = sprintf( __( 'Howdy, %s' ), '<span class="display-name">' . $current_user->display_name . '</span>' );
	$class = empty( $avatar ) ? '' : 'with-avatar';

	$wp_admin_bar->add_node(
		array(
			'id'     => 'my-account',
			'parent' => 'top-secondary',
			'title'  => $howdy,
			'href'   => $profile_url,
			'meta'   => array(
				'class' => $class,
			),
		)
	);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment