0

I'm building a contact information plugin that writes data to a page. So far, so good. What I would like is to build a function into the plugin that adds a navigation item to the main nav bar on the site (there will only ever be one nav bar at a time), and ensures the item is at the bottom of the nav bar.

1
  • Welcome to WPSE. Glad to have you here. Please make sure to mark the correct answer as accepted. Thanks! Commented Oct 7, 2012 at 1:20

1 Answer 1

2

this is the general gist of what you need to do, you'll want to add some checks in here, make sure you get a valid menu returned, etc..

$locations = get_theme_mod( 'nav_menu_locations' );
$menu = wp_get_nav_menu_object( reset( $locations ) ); // get first item in menu locations array
$menu_item_data = array(
    'menu-item-object-id' => $your_page->ID, // ID of the page you want to add
    'menu-item-parent-id' => 0,              // top level menu item
    'menu-item-position' => 0,               // setting position to 0 will add it to the end
    'menu-item-object' => 'page',
    'menu-item-type' => 'post_type',
    'menu-item-status' => 'publish'
);
wp_update_nav_menu_item( $menu->term_id, 0, $menu_item_data );
4
  • Many thanks; I'll give it a shot and return with my success. Sorry if this is a basic thing :S
    – user20517
    Commented Sep 17, 2012 at 17:21
  • Was able to add menu items automatically thanks to this code. Thank you!
    – user20517
    Commented Sep 24, 2012 at 17:52
  • @user20517 Make sure you mark this answer as accepted. Commented Oct 7, 2012 at 1:19
  • Warning: reset() expects parameter 1 to be array, boolean given Commented Oct 28, 2015 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.