title: Create Nav menu by Code in wordPress theme
body: <p>I am basically from .NET and Learning woocommerce themes ..I found this Code segment for Nav menu in Main page </p>
  
  <pre><code> &lt;?php wp_nav_menu( array( 'theme_location' =&gt; 'primary' ) ); ?&gt;
  </code></pre>
  
  <p>Edit :</p>
  
  <p>I found Navigation Menu code in /httpdocs/ECOM/wp-includes / nav-menu.php</p>
  
  <pre><code>function wp_create_nav_menu( $menu_name ) {
  
      return wp_update_nav_menu_object( 0, array( 'menu-name' =&gt; $menu_name ) );
  }
  </code></pre>
  
  <p>and this one </p>
  
  <pre><code>function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
      $menu_id = (int) $menu_id;
  
      $_menu = wp_get_nav_menu_object( $menu_id );
  
      $args = array(
          'description' =&gt; ( isset( $menu_data['description'] ) ? $menu_data['description']  : '' ),
          'name'        =&gt; ( isset( $menu_data['menu-name']   ) ? $menu_data['menu-name']    : '' ),
          'parent'      =&gt; ( isset( $menu_data['parent']      ) ? (int) $menu_data['parent'] : 0  ),
          'slug'        =&gt; null,
      );
  
      // double-check that we're not going to have one menu take the name of another
      $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
      if (
          $_possible_existing &amp;&amp;
          ! is_wp_error( $_possible_existing ) &amp;&amp;
          isset( $_possible_existing-&gt;term_id ) &amp;&amp;
          $_possible_existing-&gt;term_id != $menu_id
      ) {
          return new WP_Error( 'menu_exists',
              /* translators: %s: menu name */
              sprintf( __( 'The menu name %s conflicts with another menu name. Please try another.' ),
                  '&lt;strong&gt;' . esc_html( $menu_data['menu-name'] ) . '&lt;/strong&gt;'
              )
          );
      }
  
      // menu doesn't already exist, so create a new menu
      if ( ! $_menu || is_wp_error( $_menu ) ) {
          $menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
  
          if ( $menu_exists ) {
              return new WP_Error( 'menu_exists',
                  /* translators: %s: menu name */
                  sprintf( __( 'The menu name %s conflicts with another menu name. Please try another.' ),
                      '&lt;strong&gt;' . esc_html( $menu_data['menu-name'] ) . '&lt;/strong&gt;'
                  )
              );
          }
  
          $_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args );
  
          if ( is_wp_error( $_menu ) )
              return $_menu;
  
          do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data );
  
          return (int) $_menu['term_id'];
      }
  
      if ( ! $_menu || ! isset( $_menu-&gt;term_id ) )
          return 0;
  
      $menu_id = (int) $_menu-&gt;term_id;
  
      $update_response = wp_update_term( $menu_id, 'nav_menu', $args );
  
      if ( is_wp_error( $update_response ) )
          return $update_response;
  
      $menu_id = (int) $update_response['term_id'];
      do_action( 'wp_update_nav_menu', $menu_id, $menu_data );
      return $menu_id;
  }
  </code></pre>
  
  <p>I searched some post and write some code to update menu </p>
  
  <pre><code> wp_update_nav_menu_object($menu_id, 0, array(
          'menu-item-title' =&gt;  __('Home'),
          'menu-item-classes' =&gt; 'home',
          'menu-item-url' =&gt; home_url( '/' ), 
          'menu-item-status' =&gt; 'publish'));
  
      wp_update_nav_menu_object($menu_id, 0, array(
          'menu-item-title' =&gt;  __('Activity'),
          'menu-item-classes' =&gt; 'activity',
          'menu-item-url' =&gt; home_url( '/activity/' ), 
          'menu-item-status' =&gt; 'publish'));
  
      wp_update_nav_menu_object($menu_id, 0, array(
          'menu-item-title' =&gt;  __('Members'),
          'menu-item-classes' =&gt; 'members',
          'menu-item-url' =&gt; home_url( '/members/' ), 
          'menu-item-status' =&gt; 'publish'));
  </code></pre>
  
  <p>But this is not working .. What am I missing here ?</p>