WordPress 3.0 has been around for a while. One of its great features is the menu system. We can use the menu system out of the box from the WordPress administration screen. From there we can create a menu with flexible menu item configuration. The menu item can be configured to point and open specific post or page, specific category or tag, or custom URL. The good news for programmers: all of those things can be done programmatically. You can create a menu and its all configurable menu items via PHP code. This post will shows you some example on how to do it.
Maybe you are wondering and asking when we will need to create a WordPress menu programmatically. Admitted, there should be not so many requirements to create a menu programmatically. But I found one feasible usage: we can create a menu on the fly upon an activation of a plugin. If you are a plugin writer, you should already know that plugins have activation event. In this activation event, we can create anything: database tables, posts, pages, categories, tags, and of course the menu. This is of course assuming that the plugin needs to create those items to support its function, not just arbitrarily.
OK, now let me just jump into the sample code of creating a WordPress menu programmatically. First task is creating the menu itself. The source code below shows you an example of creating a new empty menu in WordPress 3.0:
$menu_id = wp_create_nav_menu('My Menu', array('slug' => 'my-menu'));
Second task is creating menu items programmatically. Here is a sample of a menu item that points to specific page. When the menu is clicked, the page will be opened and become active:
$post_id = wp_insert_post(array('post_title' => 'My Post', 'post_type' => 'page')); wp_update_nav_menu_item( $menu_id, 0, array( 'menu-item-title' => 'My Menu', 'menu-item-type' => 'post_type', 'menu-item-object' => 'post', 'menu-item-object-id' => $post_id, 'menu-item-position' => 1, 'menu-item-status' => 'publish') );
Here is some explanation of the code above:
- To create a new menu item, put 0 as the second parameter value in
- To point to specific post or page (or custom
'menu-item-type' => 'post_type'
- We have to know the id of the post or page to be assigned to the menu item, so in the sample code above I created a new post on the fly, get its id and assign it to the menu item. We can also retrieve existing post or page based on title or slug to get its id.
If you need the menu item to point to category or tag archive, the following source code shows an example on how to do it:
$term_id = wp_insert_category(array('cat_name' => 'My Category')); wp_update_nav_menu_item( $menu_id, 0, array( 'menu-item-title' => 'My Menu', 'menu-item-type' => 'taxonomy', 'menu-item-object' => 'category', 'menu-item-object-id' => $term_id, 'menu-item-position' => 1, 'menu-item-status' => 'publish') );
menu-item-object are different from the previous sample code. Finally, to point to specific URL, we can create a ‘custom’ menu item, as shown in the following source code:
wp_update_nav_menu_item( $menu_id, 0, array( 'menu-item-title' => 'My Menu', 'menu-item-type' => 'custom', 'menu-item-url' => '../', 'menu-item-position' => 1, 'menu-item-status' => 'publish') );
So, now we already have the menu with all its item. The third and final task is to assign the menu to a ‘theme location’. A ‘theme location’ is a concept introduced by WordPress 3.0. It is a location where we can display the menu. Theme location must be registered by theme we are using. If we use standard ‘Twenty Ten’ theme, there is a registered navigation location called ‘primary’.
The responsibility of assigning a menu into ‘theme location’ does NOT belong to navigation menu API. It is theme option. The following code shows how we can configure theme option to assign the menu we created before to a registered theme location:
$theme = get_current_theme(); $mods = get_option("mods_$theme"); $key = key($mods['nav_menu_locations']); $mods['nav_menu_locations'][$key] = $menu_id; update_option("mods_$theme", $mods);
OK, that’s all what I can give folks. I hope now you understand how to create a menu and menu items programmatically in WordPress 3.0.