walk_nav_menu_tree( array $items, int $depth, stdClass $r )

Retrieves the HTML list content for nav menu items.


Parameters Parameters

$items

(array) (Required) The menu items, sorted by each menu item's menu order.

$depth

(int) (Required) Depth of the item in reference to parents.

$r

(stdClass) (Required) An object containing wp_nav_menu() arguments.


Top ↑

Return Return

(string) The HTML list content for the menu items.


Top ↑

Source Source

File: wp-includes/nav-menu-template.php

function walk_nav_menu_tree( $items, $depth, $r ) {
	$walker = ( empty( $r->walker ) ) ? new Walker_Nav_Menu : $r->walker;

	return $walker->walk( $items, $depth, $r );
}


Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Black Swan Lab

    Add a metabox to WordPress nav-menu and add two predefined links (login, logout).

    add_action( 'admin_head-nav-menus.php', function() {
        add_meta_box( 'plugin-slug-menu-metabox', "Wordpress Login/Logout", 'wpdocs_plugin_slug_render_menu_metabox', 'nav-menus', 'side', 'default', array( /*custom params*/ ) );
    } );
    
    function wpdocs_plugin_slug_render_menu_metabox( $object, $args )
    {
      global $nav_menu_selected_id;
      // Create an array of objects that imitate Post objects
      $my_items = array(
        (object) array(
            'ID' => 1,
            'object_id' => 1,
            'type_label' => 'Login',
            'title' => 'Login',
            'url' => wp_login_url(),
            'type' => 'custom',
            'object' => 'plugin-slug-slug',
            'db_id' => 0,
            'menu_item_parent' => 0,
            'post_parent' => 0,
            'target' => '',
            'attr_title' => '',
            'description' => '',
            'classes' => array(),
            'xfn' => '',
        ),
        (object) array(
            'ID' => 1,
            'object_id' => 1,
            'type_label' => 'Logout',
            'title' => 'Logout',
            'url' => wp_logout_url(),
            'type' => 'custom',
            'object' => 'plugin-slug-slug',
            'db_id' => 0,
            'menu_item_parent' => 0,
            'post_parent' => 0,
            'target' => '',
            'attr_title' => '',
            'description' => '',
            'classes' => array(),
            'xfn' => '',
        ),
      );
    
      $db_fields = false;
      // If your links will be hierarchical, adjust the $db_fields array below
      if ( false ) { 
        $db_fields = array( 'parent' => 'parent', 'id' => 'post_parent' ); 
      }
    
      $walker = new Walker_Nav_Menu_Checklist( $db_fields );
      $removed_args = array( 'action', 'customlink-tab', 'edit-menu-item', 'menu-item', 'page-tab', '_wpnonce', );
      ?>
      <div id="plugin-slug-div">
        <div id="tabs-panel-plugin-slug-all" class="tabs-panel tabs-panel-active">
        <ul id="plugin-slug-checklist-pop" class="categorychecklist form-no-clear" >
          <?php echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $my_items ), 0, (object) array( 'walker' => $walker ) ); ?>
        </ul>
        <p class="button-controls">
          <span class="add-to-menu">
            <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-plugin-slug-menu-item" id="submit-plugin-slug-div" />
            <span class="spinner"></span>
          </span>
        </p>
      </div>
      <?php
    }
    

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