Opened 12 years ago
Last modified 3 years ago
#23805 new enhancement
wp_ajax_add_menu_item() closed to user-created menu item types
Reported by: | GaryJ | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Menus | Keywords: | has-patch |
Focuses: | Cc: |
Description
I'm building a new Menus meta box, that can add a new type of menu item, lets call it 'foobar'.
The conditional inside wp_ajax_add_menu_item()
is slightly off. It checks that the menu item type is not 'custom', then proceeds to assume that it's either post-type or taxonomy so it can do some DB look-ups and create an $_object
variable which is then used. This means it's closed to other types of menu items.
I've chosen 'foobar', so I can distinguish those items later on when walking through the front-end output.
Attachments (2)
Change History (16)
#2
@
11 years ago
I second that.
I have a plugin (http://wordpress.org/plugins/sf-archiver/) that builds a custom type, like @GaryJ. So far I couldn't find a way to avoid 2 php notices because of that problem (the variable $_object
is not set).
Another way would be to add a filter in the switch, in a default:
statement. Perhaps something like that:
default : $_object = apply_filters( 'ajax_' . $menu_item_data['menu-item-type'] . '_menu_item_object', false, $menu_item_data ); break; } if ( $_object ) { $_menu_item = wp_setup_nav_menu_item( $_object ); // Restore the missing menu item properties $menu_item_data['menu-item-description'] = $_menu_item->description; }
I guess something else would be needed for non-ajax.
#3
@
10 years ago
I was just about to report this but it seems that it's already known.
If you use custom menu-item-type's to extend the menu you will see errors like this when you try to add your custom item to the nav menu in /wp-admin/nav-menus.php
:
Notice: Undefined variable: _object in /srv/www/basetemplate/htdocs/wp-admin/includes/ajax-actions.php on line 1091 Notice: Trying to get property of non-object in /srv/www/basetemplate/htdocs/wp-admin/includes/ajax-actions.php on line 1095
Note: This is only shown while developing with define( 'WP_DEBUG', true );
Disabling JavaScript and attempting to add the custom item adds it with (Pending) and then you can save the menu without any issues so it seems like this is an ajax only issue.
It looks like the one line .diff submitted with this ticket would solve the problem.
#4
@
8 years ago
23805.2.diff - Patch updated for 4.7.
Related: #23806