add_menu_classes()WP 2.7.0

Adds CSS classes for top-level administration menu items.

The list of added classes includes .menu-top-first and .menu-top-last.

Hooks from the function

Return

Array. The array of administration menu items with the CSS classes added.

Usage

add_menu_classes( $menu );
$menu(array) (required)
The array of administration menu items.

Changelog

Since 2.7.0 Introduced.

add_menu_classes() code WP 6.6.2

function add_menu_classes( $menu ) {
	$first_item  = false;
	$last_order  = false;
	$items_count = count( $menu );

	$i = 0;

	foreach ( $menu as $order => $top ) {
		++$i;

		if ( 0 === $order ) { // Dashboard is always shown/single.
			$menu[0][4] = add_cssclass( 'menu-top-first', $top[4] );
			$last_order = 0;
			continue;
		}

		if ( str_starts_with( $top[2], 'separator' ) && false !== $last_order ) { // If separator.
			$first_item = true;
			$classes    = $menu[ $last_order ][4];

			$menu[ $last_order ][4] = add_cssclass( 'menu-top-last', $classes );
			continue;
		}

		if ( $first_item ) {
			$first_item = false;
			$classes    = $menu[ $order ][4];

			$menu[ $order ][4] = add_cssclass( 'menu-top-first', $classes );
		}

		if ( $i === $items_count ) { // Last item.
			$classes = $menu[ $order ][4];

			$menu[ $order ][4] = add_cssclass( 'menu-top-last', $classes );
		}

		$last_order = $order;
	}

	/**
	 * Filters administration menu array with classes added for top-level items.
	 *
	 * @since 2.7.0
	 *
	 * @param array $menu Associative array of administration menu items.
	 */
	return apply_filters( 'add_menu_classes', $menu );
}