Add a node with an icon to the WordPress admin bar menu

/**
 * Adds "Drafts" link to the admin bar.
 *
 * @see https://wpcodebook.com/wordpress-admin-bar-add-node-with-icon/
 * @see https://developer.wordpress.org/reference/hooks/admin_bar_menu/
 * @see https://developer.wordpress.org/reference/classes/wp_admin_bar/add_node/
 * @see https://developer.wordpress.org/resource/dashicons/
 */
add_action( 'admin_bar_menu', function ( $wp_admin_bar ) {

	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}

	$icon = '<style>
		#wpadminbar #wp-admin-bar-drafts > .ab-item:before {
			content: "\f109";
			top: 2px;
		}
	</style>';

	$wp_admin_bar->add_node( array(
		'id'    => 'drafts',
		'title' => esc_html__( 'Drafts' ),
		'href'  => admin_url( 'edit.php?post_status=draft&post_type=post' ),
		'meta'  => array(
			'html' => $icon,
		),
	) );

}, PHP_INT_MAX );

Leave a Comment