Create Menu on Theme Activation – WordPress

Clean_lava_lamp_navigation

add_action('after_switch_theme', 'theme_activation_function', 10 ,  2);

function theme_activation_function ($oldname, $oldtheme = false) {
/* Create header and footer menus */
  $menus = array(
    'Primary Menu'  => array(
      'about-us'  => 'About Us', 
      'services'  => 'Services', 
      'templates-forms'  => 'Templates & Forms', 
      'clients'  => 'Clients', 
      'price-quote'  => 'Price Quote'
    ), 
  'Top Menu' => array(
    'home' => 'Home', 
    'contact' => 'Contact', 
    'sitemap' => 'Sitemap'
  ),
  'Footer Menu' => array(
    'terms-of-use' => 'Terms of Use', 
    'privacy-policy' => 'Privacy Policy'
  )
);
foreach($menus as $menuname => $menuitems) {
  $menu_exists = wp_get_nav_menu_object($menuname);
  // If it doesn't exist, let's create it.
  if ( !$menu_exists) {
    $menu_id = wp_create_nav_menu($menuname);
    foreach($menuitems as $slug => $item) {
      wp_update_nav_menu_item(
      $menu_id, 0, array(
            'menu-item-title'  => $item,
            'menu-item-object'  => 'page',
            'menu-item-object-id'  => get_page_by_path($slug)->ID,
            'menu-item-type' => 'post_type',
            'menu-item-status'  => 'publish'
        )
      );
    }
  }
}

}


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s