Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Welcome to WPSE. Glad to have you here. Please make sure to mark the correct answer as accepted. Thanks! –  Brian Fegter Oct 7 '12 at 1:20

1 Answer 1

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 );
share|improve this answer
    
Many thanks; I'll give it a shot and return with my success. Sorry if this is a basic thing :S –  user20517 Sep 17 '12 at 17:21
    
Was able to add menu items automatically thanks to this code. Thank you! –  user20517 Sep 24 '12 at 17:52
    
@user20517 Make sure you mark this answer as accepted. –  Brian Fegter Oct 7 '12 at 1:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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