logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$35
create a custom menu item

i have a custom menu
its ID=19

i duplicate this menu and i want to populate this new menu with the items of the first menu (id=19)

i use this code


$menu_id=19;
$menu_items = wp_get_nav_menu_items($menu_id);

$new_name "another menu";
$new_menu = wp_update_nav_menu_object( 0, array( 'menu-name' => $new_name ) );
$new_menu = wp_get_nav_menu_object($new_menu);


$new = array();

foreach($menu_items as $menu_item) {

$new = array(
'menu-item-db-id' => $menu_item->db_id,
'menu-item-object-id' => 0,
'menu-item-object' => '',
'menu-item-parent-id' => $menu_id,
'menu-item-position' => $menu_item->menu_order,
'menu-item-type' => $menu_item->type,
'menu-item-title' => $menu_item->post_title,
'menu-item-url' => $menu_item->url,
'menu-item-description' => $menu_item->post_content,
'menu-item-attr-title' => $menu_item->post_excerpt,
'menu-item-target' => '',
'menu-item-classes' => '',
'menu-item-xfn' => '',
'menu-item-status' => 0,
);

wp_update_nav_menu_item($new_menu->term_id, $menu_item_db_id = 0, $new);
}


the result is a menu with an invalide item
Look at the attachment.

probably there is a problem inthe mapping of array $menu to array $new
but i'm tired and i don't see the error.

Thx for your help :-)

This question has been answered.

attachment image View Attachment

Sébastien | French WordpressDesigner | 12/17/11 at 11:54am Edit

Previous versions of this question: 12/17/11 at 12:47pm

(5) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    12/17/11
    12:18pm
    designchemical says:

    shouldnt:

    'menu-item-db-id' => $menu_item->db_id

    be


    'menu-item-db-id' => $menu_item->ID

    Previous versions of this answer: 12/17/11 at 12:18pm

    • 12/17/11 12:44pm

      Sébastien | French WordpressDesigner says:

      i replaced

      'menu-item-db-id' => $menu_item->db_id

      by
      'menu-item-db-id' => $menu_item->ID

      Nothing has changed


      if i change too
      wp_update_nav_menu_item($new_menu->term_id, 0, $new);

      to
      wp_update_nav_menu_item($new_menu->term_id, $menu_item->ID, $new);

      no item populate the new menu

  • avatar
    Last edited:
    12/17/11
    12:15pm
    Navjot Singh says:

    Check this line:

    'menu-item-parent-id' => $menu->term_id,


    Shouldn't it be

    'menu-item-parent-id' => $menu_item->term_id,


    ?

  • avatar
    Last edited:
    12/17/11
    12:26pm
    Francisco Javier Carazo Gil says:

    Sébastien,

    I think you cannot repeat in same DB the same menu-item-db-id, so:

    'menu-item-db-id' => $menu_item->db_id


    Should be:
    'menu-item-db-id' => 0


    The constructor accepts 0 (http://hitchhackerguide.com/2011/02/12/wp_update_nav_menu_item/) to give his own value.

    Tell me.

  • avatar
    Last edited:
    12/17/11
    12:44pm
    Kannan C says:

    try this


    $menu_id=19;
    $menu_items = wp_get_nav_menu_items($menu_id);
    $new_name = "another menu";
    $new_menu = wp_update_nav_menu_object( 0, array( 'menu-name' => $new_name ) );
    $new_menu = wp_get_nav_menu_object($new_menu);
    $new = array();

    foreach($menu_items as $menu_item) {

    $new = array(
    'menu-item-db-id' => 0,
    'menu-item-object-id' => 0,
    'menu-item-parent-id' => $menu_item->post_parent,
    'menu-item-position' => $menu_item->menu_order,
    'menu-item-title' => $menu_item->title,
    'menu-item-url' => $menu_item->url,
    'menu-item-description' => $menu_item->post_content,
    'menu-item-attr-title' => $menu_item->post_excerpt,
    'menu-item-target' => ''
    );
    wp_update_nav_menu_item($new_menu->term_id, $menu_item_db_id = 0, $new);

    }

    • 12/17/11 1:00pm

      Sébastien | French WordpressDesigner says:

      Nice.

      You have changed,
      'menu-item-db-id' => 0,
      and
      'menu-item-parent-id' => $menu_item->post_parent,

      Is that correct ?



      Now, there is yet a valid item.

      But there are two problems :
      -this item is a custom (the original item is a category)
      -the status of this item is not "publish"

    • 12/17/11 1:16pm

      Kannan C says:

      first thing i noticed is

      'menu-item-title' => $menu_item->title

      i am not sure about menu type, you can try using menu-item-type in the array (let me google for correct value for that. For publish simply include
      'menu-item-status' => 'publish'

    • 12/17/11 1:22pm

      Sébastien | French WordpressDesigner says:

      'menu-item-status' => 'publish' seems to work :-))

      But the is a problem :
      -this item is a custom (the original item is a category)

    • 12/17/11 1:50pm

      Sébastien | French WordpressDesigner says:

      i have added

                           'menu-item-object' => $menu_item->object,
      'menu-item-type' => $menu_item->type

      and this problem seems resolved
      but there is a big problem with the order
      if i drag and drop utems, the ordre is not well saved

    • 12/17/11 2:03pm

      Kannan C says:

      this is working fine for me. verified the re-order.


      $new = array(
      'menu-item-db-id' => 0,
      'menu-item-object-id' => $menu_item->object_id,
      'menu-item-object' => $menu_item->type_label,
      'menu-item-type' => $menu_item->type,
      'menu-item-parent-id' => $menu_item->post_parent,
      'menu-item-position' => $menu_item->menu_order,
      'menu-item-title' => $menu_item->title,
      'menu-item-url' => $menu_item->url,
      'menu-item-description' => $menu_item->post_content,
      'menu-item-attr-title' => $menu_item->post_excerpt,
      'menu-item-status' => 'publish',
      'menu-item-target' => ''
      );

    • 12/17/11 2:09pm

      Sébastien | French WordpressDesigner says:

      Are you sur it's ok for you ?
      have you test with several items (custom, category and page) ?

      'menu-item-object' => $menu_item->type_label,
      seems to be wrong
      i replace by 'menu-item-object' => $menu_item->object,

    • 12/17/11 2:21pm

      Kannan C says:

      type_label and object is both working. Go with object.
      yes its saving as where it is dropped. check screenshot

      Attached Image

    • 12/17/11 3:07pm

      Sébastien | French WordpressDesigner says:

      No
      in french "type_label" return "catégorie" and "object" return "category"
      if i use type_label that doesn't work

    • 12/17/11 3:10pm

      Sébastien | French WordpressDesigner says:

      i test the re-order in another site, a new installation of wordpress, and that doesn't work

      Delete your menu and let the function create another menu.
      And try to re-ordre the items : put a page between two categories
      (don't place an item as a children of another item)

    • 12/17/11 10:56pm

      Kannan C says:

      ok. i tested in a new installation and included a page menu btn categories, it's saved correctly. I don't know when you are duplicating the menu. But hope you do this on single action, let say on theme activation.


      if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' ) {
      $menu_id=24;

      $menu_items = wp_get_nav_menu_items($menu_id);
      //return $menu_items;
      $new_name = "another menu";

      $new_menu = wp_update_nav_menu_object( 0, array( 'menu-name' => $new_name ) );

      $new_menu = wp_get_nav_menu_object($new_menu);
      $new = array();

      foreach($menu_items as $menu_item) {

      $new = array(
      'menu-item-db-id' => 0,
      'menu-item-object-id' => $menu_item->object_id,
      'menu-item-object' => $menu_item->type_label,
      'menu-item-type' => $menu_item->type,
      'menu-item-parent-id' => $menu_item->post_parent,
      'menu-item-position' => $menu_item->menu_order,
      'menu-item-title' => $menu_item->title,
      'menu-item-url' => $menu_item->url,
      'menu-item-description' => $menu_item->post_content,
      'menu-item-attr-title' => $menu_item->post_excerpt,
      'menu-item-status' => 'publish',
      'menu-item-target' => ''
      );
      wp_update_nav_menu_item($new_menu->term_id, $menu_item_db_id = 0, $new);

      }
      }

      Attached Image

  • avatar
    Last edited:
    12/17/11
    11:11pm
    Christianto says:

    Sébastien,

    If you duplicate menu, you have to update the parent id of each menu created so the structure remain same but with the correct id.

    since each menu rely on $menu_item->menu_item_parent to create the hierarchy and each time you create a new menu item, it also create new id so all should be updated.

    try this

    $menu_id=19;
    $menu_items = wp_get_nav_menu_items($menu_id);

    $new_name = "another menu";
    $new_menu = wp_update_nav_menu_object( 0, array( 'menu-name' => $new_name ) );
    $new_menu = wp_get_nav_menu_object($new_menu);

    $new = array();
    $menu_new_id = array();

    foreach($menu_items as $menu_item) {

    if($menu_item->menu_item_parent != 0)
    $parent = $menu_new_id[$menu_item->menu_item_parent];
    else
    $parent = 0;
    $new = array(
    'menu-item-db-id' => 0,
    'menu-item-object-id' => $menu_item->object_id,
    'menu-item-object' => $menu_item->type_label,
    'menu-item-parent-id' => $parent,
    'menu-item-position' => $menu_item->menu_order,
    'menu-item-type' => $menu_item->type,
    'menu-item-title' => $menu_item->post_title,
    'menu-item-url' => $menu_item->url,
    'menu-item-description' => $menu_item->post_content,
    'menu-item-attr-title' => $menu_item->post_excerpt,
    'menu-item-target' => '',
    'menu-item-classes' => '',
    'menu-item-xfn' => '',
    'menu-item-status' => 0,
    );
    $menu_new_id[$menu_item->ID] = wp_update_nav_menu_item($new_menu->term_id, 0, $new);
    }

This question has expired.



Gabriel Reguly, Sébastien | French WordpressDesigner, Julio Potier, Francisco Javier Carazo Gil voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.