Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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_menu_page( 'Author Discussion', 'Discussion', 'manage_options', 'author_discuss', 'author_discuss_build_page', plugins_url( 'author-discussion/images/icon.png' ), 999 );
    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_build_page(){
    //do something
}

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."

share|improve this question
    
I managed to solve the issue. Turns out having the add_submenu_page() call outside of the same function was causing an error. Create your menu creation (adding of menu/submenu items) in a single function, otherwise you may encounter this odd behavior. –  Brandon White Mar 7 at 4:42

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
This is pretty much exactly what I have. I have edited my code to include the add_menu_page() function. –  Brandon White Mar 7 at 3:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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