2

I want to create my custom nav menu using a script in a wp plugin. I'm almost there using this tutorial. All I need is the option to delete menu entries.

something like wp_delete_nav_menu_item($menuID, $itemID);

As an alternative, I could create a new menu using wp_create_nav_menu($menu_name); so I don"t have to delete the default entries of the menu I would be working with otherwise. The problem here is, that the theme gives options for the position of the menu like 'header' and I dont know how i can define a position for a newly created menu.

Hope someone can give advice so I can solve one of these issues. Thanks!

This is the code I am working with so far. I need the code for the part to delete the default entries

//Add Menu
//Get Menu ID
$mymenu = wp_get_nav_menu_object('Header');
$menuID = $mymenu->term_id;

//Check if Menu exists
if( !$mymenu ) {
    //Menu exists -> Delete the default entries
    //
} else {
    //Menu does not exist -> Create it
    $menuID = wp_create_nav_menu('Header');
}

//Create new Menu Entries
//Create Parent Menu Entries
$myPage = get_page_by_title( 'Home' );
$itemData =  array(
'menu-item-object-id'   => $myPage->ID,
'menu-item-parent-id'   => 0,
'menu-item-position'    => 1,
'menu-item-object'      => 'page',
'menu-item-type'        => 'post_type',
'menu-item-status'      => 'publish'
);
wp_update_nav_menu_item($menuID, 0, $itemData);

1 Answer 1

0

Here you go.

//This code will register a Location with wordpress.
//You can assign menu to this location.
add_action('init','register_my_menu');
function register_my_menu(){
    register_nav_menus(array(
        'header_navigation' => 'Header Navigation'
        ));
}

But we haven't defined any location in theme yet.

This code will be used in theme where you want your menu to get rendered.

<?php

wp_nav_menu($menu_args);
    $menu_args = array(
        'theme_location'=> 'header_navigation', //slug from the previous code.
        'container'     =>  true,   //if you want a div around your menu.
        'menu_class'    => 'nav',   // It will add the class name of menu <ul>
        );
 ?>
1
  • I'm sorry it seems like i haven't described very well what I need. I am using an existing theme and I'm coding a plugin to create a menu as part of a setup routine. See my edit above
    – Philip
    Commented Mar 6, 2014 at 10:07

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.