Make WordPress Core

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's profile 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)

23805.diff (1.5 KB) - added by GaryJ 12 years ago.
23805.2.diff (657 bytes) - added by GaryJ 8 years ago.
Updated for 4.7-dev

Download all attachments as: .zip

Change History (16)

@GaryJ
12 years ago

#2 @GregLone
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.

Last edited 11 years ago by GregLone (previous) (diff)

#3 @rtpHarry
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.

@GaryJ
8 years ago

Updated for 4.7-dev

#4 @GaryJ
8 years ago

23805.2.diff - Patch updated for 4.7.

#5 @SergeyBiryukov
8 years ago

  • Milestone changed from Awaiting Review to 4.7

#6 @jorbin
8 years ago

@SergeyBiryukov Anything blocking you from landing this?

This ticket was mentioned in Slack in #core by stevenkword. View the logs.


8 years ago

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


8 years ago

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


8 years ago

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


8 years ago

This ticket was mentioned in Slack in #core by helen. View the logs.


8 years ago

#12 @helen
8 years ago

  • Keywords 4.8-early added
  • Milestone changed from 4.7 to Future Release

Sorry this got overlooked this cycle. This is an enhancement IMO, and a bit late to find an owner for 4.7 in any case, so punting.

#13 @helen
8 years ago

  • Type changed from defect (bug) to enhancement

#14 @johnbillion
3 years ago

  • Keywords 4.8-early removed
Note: See TracTickets for help on using tickets.