0

I wanted to create an admin menu in WordPress that does not go to a page when you click it. The Menu would be as follows.

Members <-- Top Level
View Members
Add Members

"Members", the top level menu, would not link to anywhere. It just holds the other menu items.

The reason WHY, is that different users will have different capabilities. Some will be able to add members, but not view them, and vice-versa.

Thanks for any help! I've been beating my head against the wall.

  • Quick and dirty method: add the menu item as a custom Link with a URL of # – vancoder Dec 8 '14 at 23:03
  • This creates a duplicate sub-menu item that when clicks goes nowhere. This would just get confusing. The menu would have "members", "view members' and "add members" in addition to the top level "members". I had considered this solution. – Jeff Dec 10 '14 at 10:37
0

You can use add_menu_page() to add the link yourself. Simply use # as menu slug. You can then use add_submenu_page() to add the other menu items yourself.

Another option would be to to fiddle with the global $menu array. Take this answer as base.

Edit: As per the comments, there needs to a top & sub menu entry

The following demo (mu-)plugin shows how you can add and remove and therefore custom rebuild your menu entries. The "magic" is using the global submenu, which gets looped through inside _wp_menu_output() in ~/wp-admin/menu-header.php. The filter that runs right before this function is the parent_file filter. Use it to intercept and alter the global. Doing so, you can safely remove the first menu entry by using remove_submenu_page(), while still having the default core behavior of a submenu items being visible when the main menu item is clicked.

(Note: A custom menu_icon can be set during post type registration.)

<?php
/** Plugin Name: (#170620) Menu Pages tests */

// Rebuild the Menu
add_action( 'admin_menu', function()
{
    $slug = 'edit.php?post_type=members';
    remove_submenu_page( $slug, $slug );
} );

// Set the Main menu items anchor back to the list page
add_filter( 'parent_file', function( $parent_file )
{
    $slug = 'edit.php?post_type=members';
    $GLOBALS['submenu'][ $slug ][10][2] = $slug;

    return $parent_file;
} );

// Register the "members" post type
add_action( 'wp_loaded', function()
{
    register_post_type( 'members', [
        'public'    => true,
        'menu_icon' => 'dashicons-universal-access',
        'labels'    => [
            'name'               => 'Members',
            'singular_name'      => 'Member',
            'add_new'            => 'Add New',
            'add_new_item'       => 'Add New Member',
            'edit_item'          => 'Edit Member',
            'new_item'           => 'New Member',
            'view_item'          => 'View Member',
            'search_items'       => 'Search Members',
            'not_found'          => 'No members found.',
            'not_found_in_trash' => 'No members found in Trash.',
            'parent_item_colon'  => null,
            'all_items'          => 'All Members',
        ],
    ] );
} );
  • I considered that. The problem is that it creates a menu item underneath (a duplicate version of itself). So as an option, it's not very pretty. Would have to clean it up with javascript (which I could just rely on in the first place). – Jeff Dec 10 '14 at 10:03
  • @Jeff This is something that you could have mentioned in your question. See the updated answer. – kaiser Dec 10 '14 at 14:41
  • I definitely learned something new today Kaiser, thanks for the help. – Jeff Dec 10 '14 at 17:24
0

I figured out a very simple solution based off of this question.

Just using some simple CSS. Using the class that WordPress assigns to the menu item, I can use CSS to prevent clicking. Just enqueue the style (or add it to another sheet). The class wordpress assigns to the top menu item is:

"toplevel_page_"+slug

The CSS is as follows:

a.toplevel_page_members{ pointer-events: none; }

where 'members' is the menu slug.

Then I can use add_submenu_page with the same slug as the add_menu_page and change the capability!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.