I’m trying to develop a plugin that needs to add two pages to the dashboard side-bar menu. One of these being a main category. Everything is fine, except for the second submenu item for the settings page. After perusing just about as much of the codex and searching the web for similar issues I’ve come up with no solution.

Am I missing something with this? (first submenu is to create the duplicate landing page, of course)

Code

add_action('admin_menu', 'my_add_submenu');
function my_add_submenu(){
    add_submenu_page( 'author_discuss', 'Author Discussion', 'Author Discussion', $menu_capability, 'author_discuss' );
    add_submenu_page( 'author_discuss', 'Author Discussion Settings', 'Settings', 'manage_options', 'author_discuss_settings', 'author_discuss_settings_page' );
}
function author_discuss_settings_page() { 
    //do something
}

Effect

One thing I notice, when I go to click on the link in the side-bar it redirects to /wp-admin/author_discuss_settings instead of /wp-admin/admin.php?page=author_discuss_settings. I feel like I’m missing something obvious.

What Have I tried?

I followed the final example on the WordPress Codex located here.

I have tried changing the action from admin_menu to admin_init to see if that would change the result. The URL prints as expected then, /wp-admin/admin.php?page=author_discuss_settings but throws a “You do not have sufficient permissions error.”


Solution:

On the same page in codex you have this :

NOTE: If you're running into the "You do not have sufficient permissions to access this page." message in a wp_die() screen, then you've hooked too early.

So it answers to you question partly.

The second part, the following code should work :

add_action('admin_menu', 'my_add_submenu');
function my_add_submenu(){
      add_menu_page( 'Parent', 'Parent', 'manage_options', 'author_discuss', 'author_parent' ); 
      add_submenu_page( 'author_discuss', 'Author Discussion', 'Author Discussion', 'manage_options', 'author_discuss' );
      add_submenu_page( 'author_discuss', 'Author Discussion Settings', 'Settings', 'manage_options', 'author_discuss_settings', 'author_discuss_settings_page' );
}
function author_parent() {
}
function author_discuss_settings_page() { 
    //do something
}

Check you functions and see if there's some typo.


Related Wordpress search:

, , , ,

Wordpress related questions and answers: