1

I have this:

public function add_results_page()
{

    $results_page = add_menu_page(__("Results", 'kea'), __("Results", 'kea'), 'edit_published_posts', "results", null
    , 'dashicons-admin-settings', 77);

    add_submenu_page("results", "Classes - Results", "Classes - Results", "edit_published_posts", "class_results", 
        array($this, 'load_class_results'), 79);

}

What this is intended to produce is:

Results
  => Classes - Results

The 'null' as the callback is supposed to make the top level menu item do nothing. But, in my case it produces an unhandled link to wp-admin/results. How can I achieve my aim?

1
  • generally the first submenu page is the main menu page, so normally when people have this problem they're asking how to rename the first submenu. I'm not sure a menu page that has no link is even possible. This comment might be interesting/useful for you developer.wordpress.org/reference/functions/add_menu_page/โ€ฆ Commented yesterday

1 Answer 1

3

The 'null' as the callback is supposed to make the top level menu item do nothing.

You can't! That link will always lead somewhere and that cannot be avoided ( and would make no sense and have accessibility issues if you somehow achieved it anyway ).

Instead create a menu page and a submenu page that have the same identifier. This way when you click on the main link the user finds themself on the first submenu page. This is how most people do it, how the menu is designed, and how post types and settings already work.

e.g. it might look something like this:

$menu_slug = 'you-menu-slug';
add_menu_page(
    "Results",
    'WP Docs Orders',
    'edit_published_posts',
    $menu_slug,
    false
);
add_submenu_page(
    $menu_slug,
    "Classes - Results",
    "Classes - Results",
    'edit_published_posts',
    $menu_slug,
    [ $this, 'load_class_results' ]
);

I would also note, that the use of null may actually trigger PHP deprecation warnings in some newer versions of PHP, and even fatal errors in newer ones still.

1
  • Thanks. I looked at how WordPress itself does this can reached the conclusion you have just explained to me, with your helpful code examples. Also point noted about null. Commented yesterday

Your Answer

By clicking โ€œPost Your Answerโ€, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.