WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 4 weeks ago

#19085 new defect (bug)

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

Reported by: mikeschinkel Owned by:
Milestone: Awaiting Review Priority: normal
Severity: major 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 (1)

19085.patch (1.1 KB) - added by pentatonicfunk 6 months ago.

Download all attachments as: .zip

Change History (13)

comment:1 mikeschinkel2 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

comment:2 ocean902 years ago

  • Version changed from 3.3 to 3.2

comment:3 follow-up: ocean902 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 2 years ago by ocean90 (previous) (diff)

comment:4 in reply to: ↑ 3 mikeschinkel2 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 2 years ago by mikeschinkel (previous) (diff)

comment:5 rmarks9 months 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

comment:6 plocha7 months 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 7 months ago by plocha (previous) (diff)

comment:7 plocha7 months ago

#25205 was marked as a duplicate.

comment:8 eclare6 months 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.

comment:9 kpdesign6 months ago

  • Keywords needs-patch added; has-patch removed

Fixing keywords, no patches currently attached to this ticket.

pentatonicfunk6 months ago

comment:10 pentatonicfunk6 months ago

  • Keywords needs-testing added; needs-patch removed

don't be harsh, my first patch

comment:11 kpdesign6 months ago

  • Keywords has-patch added
Note: See TracTickets for help on using tickets.