plugin development - Is this best practice for Dynamically adding items to WordPress menus?

admin2025-01-08  6

Is there an easier way too add this functionality to the menus page, instead of using the following code? Another words a different filter/hook/action that will make it, so I don't have to write custom JS to get the current order?

Just to be clear this code currently only adds an new link too the nav-menus.php page called books, But what I would like to be able to do is sort the items in the current menu without having to hack something together when there may be something that is more common practice to use that will get an save the order of the menu items when they are edited.

    add_filter( 'wp_get_nav_menu_items', 'custom_nav_menu_items', 20, 2 );
/**
 * Simple helper function for make menu item objects
 * 
 * @param $title      - menu item title
 * @param $url        - menu item url
 * @param $order      - where the item should appear in the menu
 * @param int $parent - the item's parent item
 * @return \stdClass
 */ 
function _custom_nav_menu_item( $title, $url, $order, $parent = 0 ){
  $item = new stdClass();
  $item->ID = 1000000 + $order + parent;
  $item->db_id = $item->ID;
  $item->title = $title;
  $item->url = $url;
  $item->menu_order = $order;
  $item->menu_item_parent = $parent;
  $item->type = '';
  $item->object = '';
  $item->object_id = '';
  $item->classes = array();
  $item->target = '';
  $item->attr_title = '';
  $item->description = '';
  $item->xfn = '';
  $item->status = '';
  return $item;
}

function custom_nav_menu_items( $items, $menu ){
  // only add item to a specific menu
  $items[] = _custom_nav_menu_item( 'Books', get_post_type_archive_link('books'), 100 ); 
  return $items;
}

Any Direction from someone experienced in this type of thing would be great, thanks.

Is there an easier way too add this functionality to the menus page, instead of using the following code? Another words a different filter/hook/action that will make it, so I don't have to write custom JS to get the current order?

Just to be clear this code currently only adds an new link too the nav-menus.php page called books, But what I would like to be able to do is sort the items in the current menu without having to hack something together when there may be something that is more common practice to use that will get an save the order of the menu items when they are edited.

    add_filter( 'wp_get_nav_menu_items', 'custom_nav_menu_items', 20, 2 );
/**
 * Simple helper function for make menu item objects
 * 
 * @param $title      - menu item title
 * @param $url        - menu item url
 * @param $order      - where the item should appear in the menu
 * @param int $parent - the item's parent item
 * @return \stdClass
 */ 
function _custom_nav_menu_item( $title, $url, $order, $parent = 0 ){
  $item = new stdClass();
  $item->ID = 1000000 + $order + parent;
  $item->db_id = $item->ID;
  $item->title = $title;
  $item->url = $url;
  $item->menu_order = $order;
  $item->menu_item_parent = $parent;
  $item->type = '';
  $item->object = '';
  $item->object_id = '';
  $item->classes = array();
  $item->target = '';
  $item->attr_title = '';
  $item->description = '';
  $item->xfn = '';
  $item->status = '';
  return $item;
}

function custom_nav_menu_items( $items, $menu ){
  // only add item to a specific menu
  $items[] = _custom_nav_menu_item( 'Books', get_post_type_archive_link('books'), 100 ); 
  return $items;
}

Any Direction from someone experienced in this type of thing would be great, thanks.

Share Improve this question edited Apr 19, 2019 at 8:16 Krzysiek Dróżdż 25.5k9 gold badges53 silver badges74 bronze badges asked Apr 19, 2019 at 6:42 Daniel WrightDaniel Wright 114 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It's always hard to say which way to solve some problem is the best, because it's based on opinions and believes. But...

There are 3 ways I would use (depending on context and needs) to add such menu item:

1. Using wp_get_nav_menu_items filter

Exactly the way you're doing in your question. The only problem with it is that you have to be careful and fill all needed fields of the $item object. Otherwise it may cause warnings and notices.

PS. The wp_nav_menu_objects filter is pretty similar and it's also used for this purpose very often.

2. Using wp_nav_menu_items filter to append some items

This filter gets a string containing HTML code for menu items. So it's pretty easy to append some HTML code to given menu. The items you add this way won't be visible/editable in wp-admin.

So it's a pretty good idea to use it, if you want to add some special links to specific menu, that should always be at the beginning or at the end - let's say Login/Logout link...

3. Using one of above filters to rewrite some menu items

It's another pretty common practice. Let's say you want to put a Login/Logout link and allow user to edit its label and position. But of course you don't want force user to edit its URL.

In such way you can add some placeholder as URL and then use the filter to rewrite it. For example you can add "Sign in | Sign out" as label and "#loginouturl" as URL, and then use filters to change the label and url based on current user (if his logged in or anonymous).

Every one of these solutions has its good and bad sides. Some of them are better in some cases.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736268004a1239.html

最新回复(0)