Feb
23
2014
23
2014
Code to Add a New Menu Item Type
My latest diversion.
The code at this link adds a new meta box on the Appearance>Menu screen. The defaults are Page, Custom, and Category. This particular one adds a divider type. It doesn’t have any code beyond getting the meta box to show. There is a plugin that adds a custom menu item called Dynamic Menu Item that could be edited to see how it pulls it off.
I’m looking mostly for my own curiosity but I was also trying to find out what it would take, or if it was even possible, to add a dynamic menu that shows all the sub-sites in a multi-site install.
The relevant comment says:
I did create the metabox and displays where it should. Code below(the display part is just copied from Custom Link).
//handler for adding dividers
function add_divider_function()
{
global $_nav_menu_placeholder, $nav_menu_selected_id;
$_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
$current_tab = 'create';
if ( isset( $_REQUEST['customlink-tab'] ) && in_array( $_REQUEST['customlink-tab'], array('create', 'all') ) ) {
$current_tab = $_REQUEST['customlink-tab'];
}
$removed_args = array(
'action',
'customlink-tab',
'edit-menu-item',
'menu-item',
'page-tab',
'_wpnonce',
);
?>
<div class="customlinkdiv" id="customlinkdiv">
<input type="hidden" value="custom" name="menu-item[<?php echo $_nav_menu_placeholder; ?/>][menu-item-type]" />
<p id="menu-item-name-wrap">
<label class="howto" for="custom-menu-item-name">
<span>< ?php _e('Title'); ?></span>
<input id="custom-menu-item-name" name="menu-item[<?php echo $_nav_menu_placeholder; ?/>][menu-item-title]" type="text" class="regular-text menu-item-textbox input-with-default-title" title="< ?php esc_attr_e('Leave blank for empty divider'); ?>" />
</label>
</p>
<p class="button-controls">
<span class="add-to-menu">
<img decoding="async" class="waiting" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?/>" alt="" />
<input type="submit"<?php disabled( $nav_menu_selected_id, 0 ); ?/> class="button-secondary submit-add-to-menu" value="< ?php esc_attr_e('Add a Divider to the Menu'); ?>" name="add-custom-menu-item" id="submit-customlinkdiv" />
</span>
</p>
</div><!-- /.customlinkdiv -->
< ?php
}
//configure admin menus
function configure_admin_menus()
{
remove_meta_box('add-category','nav-menus','side');
add_meta_box('add-divider','Divider','add_divider_function','nav-menus','side', 'default');
}
add_action( 'admin_head', 'configure_admin_menus', 99 );
Another relevant post and snippet.
What I ended up doing for this, was adding a new post type that was hidden everywhere on the site except the nav menus. I then added just a single entry of that post type type, and hid some of the fields.
This allows the user to add this as a normal menu item. This won’t play well with functions that build the menu markup for you, but if you traverse the menu item and build the markup for you, you can target this post type with custom markup.
It looks like another way to handle menus is to add a filter to wp_get_nav_menu_items. This seems less “correct” to me for something like a Sites menu but the info is probably relevant. Also includes discussion of writing a custom menu item Walker class.
Link dump:
http://teleogistic.net/2013/02/dynamically-add-items-to-a-wp_nav_menu-list/
http://stackoverflow.com/questions/11935423/how-do-i-generate-a-custom-menu-sub-menu-system-using-wp-get-nav-menu-items-in-w
http://qandasys.info/determine-which-theme-location-a-wp_get_nav_menu_items-is-for/
http://wordpress.stackexchange.com/questions/37787/adding-a-custom-post-type-into-the-menu-screen
Category Posts in Custom Menu Plugin
http://wordpress.org/support/topic/dynamic-post-category-menu?replies=5