The wp_get_nav_menu_items hook is used to filter the navigation menu items that are returned by the wp_get_nav_menu_items function. This function is responsible for retrieving the list of menu items that belong to a specific navigation menu. The wp_get_nav_menu_items function returns an array of objects that represent the menu items. These objects contain properties such as the menu item ID, title, URL, and other metadata.
By using the wp_get_nav_menu_items hook, you can modify the list of menu items that are returned by the wp_get_nav_menu_items function. This can be useful if you need to add, remove, or modify certain menu items based on specific conditions. For example, you might want to remove certain menu items for logged-in users, or add new menu items based on the user’s role.
Here’s an example usage code for the wp_get_nav_menu_items hook:
function custom_nav_menu_items( $items, $menu, $args ) {
// Modify the $items array here
return $items;
}
add_filter( 'wp_get_nav_menu_items', 'custom_nav_menu_items', 10, 3 );
In this example, we’ve created a custom function called custom_nav_menu_items
that accepts three arguments: $items
, $menu
, and $args
. The $items
argument contains the array of menu items that is returned by the wp_get_nav_menu_items function. We can modify this array as needed within our function and then return it.
Finally, we use the add_filter
function to hook our custom function into the wp_get_nav_menu_items hook. The add_filter
function takes three arguments: the name of the hook we’re targeting, the name of our custom function, and the priority of the filter. In this case, we’re setting the priority to 10 and passing in three arguments, because the wp_get_nav_menu_items hook accepts three arguments.