apply_filters( ‘wp_setup_nav_menu_item’, object $menu_item )

Filters a navigation menu item object.


The menu item object.


return apply_filters( 'wp_setup_nav_menu_item', $menu_item );



User Contributed Notes

  1. Skip to note 2 content

    In some cases when using WordPress for application development, you might need to change the url structure of a page url in the nav menu to reflect your routing setup to maintain an app-like state.

    In this example, we want to filter all nav menu links in the menu item object that are pages, and change them from the default to a prefixed slug like /pages/target-page/ to meet out routing needs. To do this, filter the menu item object like so:

    function filter_nav_menu_items($menu){
    	$post_type = ($menu->object); //gets post type
    	//if post type is a page, then create a new URL
    	if ($post_type === 'page') {
    		$current_url = $menu->url; //grab the current url link
    		$new_url  = '/pages' . str_replace( '', '/', $current_url ); //replace the base url with a '/'
    		$menu->url = $new_url; //save the new url to the <code>url</code> property in the menu item object
    	return $menu; //return the filtered object
    add_filter( 'wp_setup_nav_menu_item', 'filter_nav_menu_items', 1 );

You must log in before being able to contribute a note or feedback.