0

Here's a custom wordpress admin page I created:-

// code located in my custom plugin's php file

function my_special_function(){

    echo '<div> Hello World </div>';

}

function my_menu_page(){

    $page_title = "Hello Page Title";
    $menu_title = "Hello Menu Title";
    $capability = "administrator";
    $menu_slug = "do_something";
    $function = my_special_function;
    add_menu_page($page_title,  $menu_title, $capability, $menu_slug, $function);

}

add_action('admin_menu', 'my_menu_page');

According to http://codex.wordpress.org/Function_Reference/add_menu_page , this function takes a 'capability' (see Roles and Capabilities) which will be used to determine whether or not a page is included in the menu. The function which is hooked in to handle the output of the page must check that the user has the required 'capability' as well.

The current user I am using is the administrator, but when I attempt to access the page

http://localhost:8888/wp-admin/admin.php?page=do_something

I get

You do not have sufficient permissions to access this page.

Why's that so? How do I fix this?

2 Answers 2

1

This is what you need:

function my_special_function(){
    ?>
        <div class='wrap'>
            <h2>Hello World</h2>
            <!-- ALL THE CUSTOM MARK UP SHOULD BE INSIDE WRAP-->
        </div>
    <?php
}

function my_menu_page(){
    add_menu_page('Page title', 'Menu Title', 'administrator', 'page_slug', 'my_special_function');
}

add_action('admin_menu', 'my_menu_page');

Try this!

6
  • Unfortunately, this isn't it. I am still getting the exact same error. May 4, 2012 at 4:55
  • Any luck? I tried it locally, works neat. May 4, 2012 at 5:16
  • What version of wordpress are you using? Perhaps my version 3.2.2 has a problem with administrator rights? Or there's something specific I need to configure for my administrators? My remote site and local site are both having the same error. May 4, 2012 at 5:47
  • I'm using the latest version bro! And the code is totally version independent! May 4, 2012 at 5:58
  • 1
    You shouldn't use role names in place of capabilities. The permissions an 'administrator' has can be changed. See @fischi's answer. May 4, 2012 at 10:28
1

try using the needed capability, not the user role. note that these are two different items - a role can have capabilities, but a administrator role does not necessarily include any capabilities, as they are free to be assigned and removed.

manage_options would be a good capability for you to start with.

function my_special_function(){
    ?>
        <div class='wrap'>
            <h2>Hello World</h2>
            <!-- ALL THE CUSTOM MARK UP SHOULD BE INSIDE WRAP-->
        </div>
    <?php
}

function my_menu_page(){
    add_menu_page('Page title', 'Menu Title', 'manage_options', 'page_slug', 'my_special_function');
}

add_action('admin_menu', 'my_menu_page');
8
  • I also tried that and that does not work either. May 4, 2012 at 14:48
  • are you sure your user has the capability to 'manage_options'? if you are not sure, try 'read_posts', it is a very lowkey capability.
    – fischi
    May 4, 2012 at 14:51
  • If I use read_posts, the menu doesn't even show up on the admin side bar. I tried read again (tried previously) - same error. May 4, 2012 at 14:59
  • So to rephrase my question a little - how/where can I find out the current capability of my current user? I just know that my current user is listed as an Administrator in the "users list" page. May 4, 2012 at 15:04
  • i am sorry, the capability would be 'read'
    – fischi
    May 4, 2012 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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