Add custom fields to menu items: an icon per link
A menu item in WordPress is a post of the type nav_menu_item, so it can have its own meta. With that, editors can set an icon, a badge text or a column number per menu item right in Appearance → Menus. WordPress 5.4 added the wp_nav_menu_item_custom_fields action, which prints extra fields in the menu item form without replacing the admin walker.
Problem: Editors should choose an icon (Font Awesome classes such as fas fa-home) for each menu item, and the menu on the site should show the icon before the link text.
Solution: Print the field with wp_nav_menu_item_custom_fields, save it on wp_update_nav_menu_item, and add the icon to the link text with the nav_menu_item_title filter.
The field in the menu editor
<?php
add_action( 'wp_nav_menu_item_custom_fields', 'ha_menu_item_icon_field', 10, 2 );
function ha_menu_item_icon_field( $item_id, $item ) {
$icon = get_post_meta( $item_id, '_ha_menu_icon', true );
?>
<p class="field-ha-icon description description-wide">
<label for="edit-menu-item-ha-icon-<?php echo esc_attr( $item_id ); ?>">
<?php esc_html_e( 'Icon classes (e.g. fas fa-home)', 'textdomain' ); ?><br>
<input type="text" class="widefat"
id="edit-menu-item-ha-icon-<?php echo esc_attr( $item_id ); ?>"
name="ha-menu-icon[<?php echo esc_attr( $item_id ); ?>]"
value="<?php echo esc_attr( $icon ); ?>">
</label>
</p>
<?php
}
The field name contains the item ID, because the whole menu with all its items is saved in one form.
Saving
<?php
add_action( 'wp_update_nav_menu_item', 'ha_save_menu_item_icon', 10, 2 );
function ha_save_menu_item_icon( $menu_id, $menu_item_db_id ) {
if ( ! isset( $_POST['ha-menu-icon'][ $menu_item_db_id ] ) ) {
return; // the item was not in this form, e.g. saved from the Customizer
}
$raw = sanitize_text_field( wp_unslash( $_POST['ha-menu-icon'][ $menu_item_db_id ] ) );
$classes = array_filter( array_map( 'sanitize_html_class', preg_split( '/\s+/', $raw ) ) );
if ( $classes ) {
update_post_meta( $menu_item_db_id, '_ha_menu_icon', implode( ' ', $classes ) );
} else {
delete_post_meta( $menu_item_db_id, '_ha_menu_icon' );
}
}
sanitize_html_class() works on one class and removes spaces, so the value is split first: fas fa-home stays fas fa-home instead of becoming fasfa-home.
The icon in the menu
<?php
add_filter( 'nav_menu_item_title', function ( $title, $item ) {
$icon = get_post_meta( $item->ID, '_ha_menu_icon', true );
if ( $icon ) {
$title = '<i class="' . esc_attr( $icon ) . '" aria-hidden="true"></i> ' . $title;
}
return $title;
}, 10, 2 );
nav_menu_item_title filters the link text of every item, with any walker that uses the standard start_el(). The icon is hidden from screen readers, the text stays readable. If a link should show only the icon, keep the text for screen readers in a screen-reader-text span.
Menu locations and the menu output itself are covered in registering and displaying navigation menus, and a full custom walker in different walker output for the header and the footer.
NOTE: The Customizer’s menu panel does not show fields added with wp_nav_menu_item_custom_fields; it has its own JavaScript UI. Edit such fields in Appearance → Menus, and keep the “not in this form” check in the save function, so saving a menu in the Customizer does not delete the icons.