Search code examples
phpwordpresswordpress-themingnavgenesis

Add Custom Nav Menu Scroll Points To Onboarding Menu


I need to add scroll points to custom links for theme setup in the onboarding-shared.php file :

'navigation_menus' => [
        'primary' => [
            'getstarted'    => [
                'title' => 'Get Started',
            ],
            'testimonials'  => [
                'title' => 'Testimonials',
            ],
            'contact'       => [
                'title' => 'Contact Us',
            ],
        ],
    ],

Is it possible to hard code the links in the PHP array? I would like to add custom links like this :

#section-2

Like this :

enter image description here


Solution

  • You can use wp_create_nav_menu() and wp_update_nav_menu_item() to accomplish this:

    $menu_name = 'Primary';
    $menu_exists_primary = wp_get_nav_menu_object( $menu_name );
    
    if ( ! $menu_exists_primary  ) {
            $menu_id = wp_create_nav_menu($menu_name);
    
            wp_update_nav_menu_item( $menu_id, 0, array(
                'menu-item-title'   =>  'Get Started',
                'menu-item-url'     => '#getstarted', 
                'menu-item-status'  => 'publish'
            ) );
            
            wp_update_nav_menu_item( $menu_id, 0, array(
                'menu-item-title'   =>  'Testimonials',
                'menu-item-url'     => '#testimonials', 
                'menu-item-status'  => 'publish'
            ) );
            
            wp_update_nav_menu_item( $menu_id, 0, array(
                'menu-item-title'   =>  'Contact Us',
                'menu-item-url'     => '#contact', 
                'menu-item-status'  => 'publish'
            ) );
            
            $locations = get_theme_mod('nav_menu_locations');
            $locations['primary'] = $menu_id;
            set_theme_mod( 'nav_menu_locations', $locations );
        }