• I’m currently working through the Udemy course Complete WordPress Theme & Plugin Development Course [2021] by Zac Gordon. At section 41 on creating a menu, the code doesn’t appear to be working. I’ve tried contacting both Udemy and Zac directly about this but so far have had no response. The code in the functions.php file is as follows:

    // Register Menu Locations
    register_nav_menus( [
      'main-menu' => esc_html__( 'Main Menu', 'wphierarchy' ),
    ]);

    Any help appreciated. I’m working offline so can’t send a link.

    • This topic was modified 3 years, 7 months ago by bcworkz.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator bcworkz

    (@bcworkz)

    Is that code within an action hook callback? (action “after_setup_theme”) I added your code to my theme’s callback and it works fine after changing the key and label, since my theme already had the ones you used.

    add_action('after_setup_theme', function() {
    	// Register Menu Locations
    	register_nav_menus( [
    	  'foo-menu' => esc_html__( 'Foo Menu', 'sample' ),
    	]);
    });
    Thread Starter Robert

    (@rsterry48)

    Thanks for getting back. I don’t think that code is within an action hook callback. Unless it’s in the header.php. There’s nothing like that in the functions.php file. Here’s the complete code in functions.php. Remember I’m on my Ls.

    <?php
    
    // Add Theme Support
    add_theme_support( 'title-tag' );
    add_theme_support( 'post-thumbnails' );
    add_theme_support( 'post_format', ['aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat'] );
    add_theme_support( 'html5' );
    add_theme_support( 'automatic-feed-links' );
    add_theme_support( 'custom-background' );
    add_theme_support( 'custom-header' );
    add_theme_support( 'custom-logo' );
    add_theme_support( 'customize-selective-refresh-widgets' );
    add_theme_support( 'starter-content' );
    
    // Load in our CSS
    function wphierarchy_enqueue_styles() {
    
      wp_enqueue_style( 'main-css', get_stylesheet_directory_uri() . '/style.css', [], time(), 'all' );
    
    }
    add_action( 'wp_enqueue_scripts', 'wphierarchy_enqueue_styles' );
    
    // Register Menu Locations
    register_nav_menus( [
      'main-menu' => esc_html__( 'Main Menu', 'wphierarchy' ),
    ]);
    
    ?>
    • This reply was modified 3 years, 7 months ago by bcworkz.
    Moderator bcworkz

    (@bcworkz)

    When the theme’s code is loaded, WP is still in an unstable state. Not all functions and data are yet available. It’s very common to place functions in action hook callbacks for later execution when things are more stable. I’ve never seen in WP docs where register_nav_menus() is called outside of an action hook. All WP docs I know of have the registration done from “after_setup_theme”. I suggest you do the same. The example code I provided earlier does work as expected.

    In fact, add_theme_support() is also normally called from “after_setup_theme”. They all should be included in the same callback as well. I’m not saying adding support as it is now wouldn’t work, only that it’s abnormal to do it that way.

    Thread Starter Robert

    (@rsterry48)

    Thanks heaps. Will give it a go.

    Thread Starter Robert

    (@rsterry48)

    No. That didn’t work alas. No Main Menu created. No errors either.

    Here’s my code:

    add_action('after_setup_theme', function () {
    // Register Menu Locations
    register_nav_menus( [
      'main-menu' => esc_html__( 'Main Menu', 'wphierarchy' ),
    ]);
    });
    • This reply was modified 3 years, 7 months ago by bcworkz.
    Moderator bcworkz

    (@bcworkz)

    That looks familiar ?? I double checked your code on my site to be sure there’s no super minor syntax error that’s not readily visible. There’s not, it works fine. Be sure caching isn’t confusing the issue. Flush your browser cache and any server side caching you might have.

    Are you checking in the right pace in the back end? The code doesn’t actually add a menu itself, no code is needed for that. It adds a menu location. It’s shown on the manage locations tab, not the edit menus tab. To actually add a new menu, there’s a link near the top of the edit menus admin screen.

    Thread Starter Robert

    (@rsterry48)

    I think I’ve finally figured it out. The code is fine but the instructions in the course aren’t clear. I understood that once the code was in place in functions.php, then I wouldn’t need to also create the ‘Main Menu’ menu.

    So when I went to Appearance-Menus-Manage Locations, there was no Main Menu to select. I assumed it was the code. But once I created the menu, it worked fine.

    I’ve continued on to the next course topic and things seem to be working as expected.

    Thank you very much for your assistance. Your additional info about calling from “after_setup_theme” was most helpful.

    Moderator bcworkz

    (@bcworkz)

    You’re welcome. Yeah, you’ve fallen victim to the menu scheme terminology being rather confusing. The WP “menu” and theme “menu location” are two very different things, even if interrelated. One is made in the UI, the other made in theme code. I hope the rest of the course is less confusing for you and you learn awesome things.

    Thread Starter Robert

    (@rsterry48)

    Thank you again for all your help. Very much appreciated. Especially as neither the course teacher nor the course distributor, in this case Udemy, answered my requests for assistance.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Creating a new menu’ is closed to new replies.