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.

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?

share|improve this question
add comment

2 Answers 2

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!

share|improve this answer
    
Unfortunately, this isn't it. I am still getting the exact same error. –  Calvin Cheng May 4 '12 at 4:55
    
Any luck? I tried it locally, works neat. –  Rutwick Gangurde May 4 '12 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. –  Calvin Cheng May 4 '12 at 5:47
    
I'm using the latest version bro! And the code is totally version independent! –  Rutwick Gangurde May 4 '12 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. –  Stephen Harris May 4 '12 at 10:28
show 1 more comment

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');
share|improve this answer
    
I also tried that and that does not work either. –  Calvin Cheng May 4 '12 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 '12 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. –  Calvin Cheng May 4 '12 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. –  Calvin Cheng May 4 '12 at 15:04
    
i am sorry, the capability would be 'read' –  fischi May 4 '12 at 15:07
show 3 more comments

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.