Make WordPress Core

Opened 13 years ago

Last modified 5 years ago

#19085 new defect (bug)

Removing First Submenu Page in Admin Menu breaks URL for Menu Page

Reported by: mikeschinkel's profile mikeschinkel Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.1
Component: Administration Keywords: needs-testing has-patch
Focuses: Cc:

Description

If you attempt to remove the Post Type Submenu Page in the Admin it breaks the Menu Page URL; it causes the Menu Page URL to be the same as the new first Submenu Page URL:

http://screenshots.newclarity.net/skitched-20111029-142108.png

Here is a simple class you can drop into the theme's functions.php file to experience this bug. This example is a minimum to trigger the error (I simplified the register_post_type() call so the example code would have fewer lines):

<?php

class Trigger_Admin_Menu_Bug {
  static function on_load() {
    add_action( 'init', array( __CLASS__, 'init' ) );
    add_action( 'parent_file', array( __CLASS__, 'parent_file' ) );
  }
  static function init() {
    global $wpdb;
    register_post_type( 'test-cpt', array(
      'label'   => 'Test CPT',
      'show_ui' => true,
    ));
  }
  static function parent_file( $parent_file ) {
    remove_submenu_page( 'edit.php?post_type=test-cpt', 
                         'edit.php?post_type=test-cpt' );
    return $parent_file;
  }
}
Trigger_Admin_Menu_Bug::on_load();

I'd provide a patch but the admin menu code is more complex than I can fully understand. Maybe the person who originally wrote it could fix it?

Note: Sadly, this is a blocker for one of my client projects. The client wants the admin menus simplified to reduce the conceptual load on their end users because we are adding many other submenu pages. Plus I've traced through the core WP code with a debugger for many hours looking for hooks that would allow me to get around this issue, but there simply are no hooks where it would be needed to hack a fix for this.

Attachments (2)

19085.patch (1.1 KB) - added by pentatonicfunk 11 years ago.
submenuasparent.19085.diff (971 bytes) - added by chrisguitarguy 9 years ago.
Alternative take on filtering $submenu_as_parent

Download all attachments as: .zip

Change History (16)

#1 @mikeschinkel
13 years ago

  • Cc mikeschinkel@… added
  • Summary changed from Deleting First Submenu Page in Admin Menu breaks URL for Menu Page to Removing First Submenu Page in Admin Menu breaks URL for Menu Page

#2 @ocean90
13 years ago

  • Version changed from 3.3 to 3.2

#3 follow-up: @ocean90
13 years ago

  • Version changed from 3.2 to 3.1

This also happens for the default post post type.

add_action( 'admin_menu', 'ds_remove_first' );

function ds_remove_first() {
	remove_submenu_page( 'edit.php', 'edit.php' );
}

"If the first submenu is not the same as the assigned parent, make the first submenu the new parent."
http://core.trac.wordpress.org/browser/trunk/wp-admin/includes/menu.php#L74

Last edited 13 years ago by ocean90 (previous) (diff)

#4 in reply to: ↑ 3 @mikeschinkel
13 years ago

Replying to ocean90:

"If the first submenu is not the same as the assigned parent, make the first submenu the new parent."
http://core.trac.wordpress.org/browser/trunk/wp-admin/includes/menu.php#L74

Ugh. Good catch.

Obviously unintended side effects?

BTW, that is not the only place that takes control of the admin menus. The _wp_menu_output() function also outputs things that are different from what's in the global $submenu array. I know this because I tried to fix the $submenu array in the 'parent_file' hook that gets called before the call to _wp_menu_output(), but that only made matters worse.

Last edited 13 years ago by mikeschinkel (previous) (diff)

#5 @rmarks
11 years ago

I tested this in 3.7-alpha and found that the parent menu item honored the URL of the first submenu item. So, Test CPT and Add New both had the URL of /wp-admin/post-new.php?post_type=test-cpt

#6 @plocha
11 years ago

  • Keywords has-patch added; needs-patch removed

I wrote a patch for that issue. You can remove the first submenu entry of any menu if you add a filter which returns false. This patch does not introduce new functionality. It only make it possible to change an as yet unused parameter for '_wp_menu_output'.

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

#7 @plocha
11 years ago

#25205 was marked as a duplicate.

#8 @eclare
11 years ago

  • Cc eclare added

This is a bigger issue - one can not make a link to the Menu item that is different from the 1st Submenu item.
I know that "it's not a bug - it's a feature", but the side effect - like said above - is quite bad.
This prevents removing some submenus, for example if I want to remove the "Home" that is the 1st submenu of the Dashboard menu, I use the following code:

add_action('admin_menu', 'register_custom_admin_menus');
function register_custom_admin_menus() {
remove_submenu_page('index.php', 'index.php');
}

This will remove the "Home" menu, but as stated above, the current WP code makes it so the menu link for the Dashboard changes to the next submenu item, which is update-core.php (Update). This makes it impossible to access the Dashboard. This is just an example.

I do not believe that plocha's patch fixes this. It looks like it only allows you to remove a submenu that has the same link as the main menu (which I believe is working without this patch), but it still doesn't fix the issue that the Menu links gets the new link from the 1st Submenu page.

Therefore, I think this should be marked as needs-patch. Please correct me if I'm wrong.

#9 @kpdesign
11 years ago

  • Keywords needs-patch added; has-patch removed

Fixing keywords, no patches currently attached to this ticket.

#10 @pentatonicfunk
11 years ago

  • Keywords needs-testing added; needs-patch removed

don't be harsh, my first patch

#11 @kpdesign
11 years ago

  • Keywords has-patch added

#13 @chriscct7
9 years ago

  • Severity changed from major to normal

@chrisguitarguy
9 years ago

Alternative take on filtering $submenu_as_parent

#14 @chrisguitarguy
9 years ago

The patch I just added is an alterntive take on filtering here that I think makes sense. Keep all the behavior the same as before, which works for 99% of people. But add two filters. One to change the global $submenu_as_parent value, the other to change the beavhior on individual admin pages. Meaning you can do something like this:

add_action('admin_menu', 'wpse206471_add_pages');
function wpse206471_add_pages()
{
    $hook = add_menu_page(
        __('WPSE206471 Main Page', 'wpse206471'),
        __('Main Page', 'wpse206471'),
        'manage_options',
        'wpse206471-main',
        'wpse206471_main_page'
    );
    add_submenu_page(
        'wpse206471-main',
        __('WPSE206471 Sub Page', 'wpse206471'),
        __('Sub Page', 'wpse206471'),
        'manage_options',
        'wpse206471-sub',
        'wpse206471_sub_page'
    );

    // remove the "main" submenue page
    remove_submenu_page('wpse206471-main', 'wpse206471-main');
    // tell `_wp_menu_output` not to use the submenu item as its link
    add_filter("submenu_as_parent_{$hook}", '__return_false');
}
Version 0, edited 9 years ago by chrisguitarguy (next)
Note: See TracTickets for help on using tickets.