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 am running a fresh install of Wordpress 3.3.2 and the only plugin enabled is one I'm developing, but I can't seem to get past a permissions issue in add_menu_page. With the exception of using anonymous functions instead of named functions, I'm following the documentation almost exactly.

My plugin source:

<?php
/*
Plugin Name: Some Plugin
*/

add_action('admin_init', function() {
    add_menu_page('Some Page', 'Some Page', 'manage_options', 'some-slug', function() {
        echo 'Hello, world!';
    });
});

?>

The menu link shows up fine at the bottom of the menu, but instead of "Hello, world!", I see:

You do not have sufficient permissions to access this page.

I've also tried using the administrator capability in place of manage_options, but have the same results.

What am I doing wrong?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

You want the admin_menu hook, rather than admin_init.

Also, you shouldn't use anonymous functions. Instead, use:

function wpse51004_add_menu_page() {
    add_menu_page('Some Page', 'Some Page', 'manage_options', 'some-slug', 'wpse51004_some_page_callback');
};
add_action('admin_menu', 'wpse51004_add_menu_page');

function wpse51004_some_page_callback() {
        echo 'Hello, world!';
    }
share|improve this answer
    
Ahh, admin_menu is what I was missing. Thanks for catching that! I'll stick with the anonymous functions, though, as my target audience (me) is always on PHP 5.3+. Developing on anything less is an absolute pain and incredibly ugly. –  Kevin May 3 '12 at 11:47
    
Ok, but anonymous functions can't be 'unhooked' - so generally speaking (and certainly if for distribution) should be avoided. It's also easier to read/layout code without anonymous callbacks, but that's my personal opinion :) –  Stephen Harris May 3 '12 at 11:50
1  
Hmmm, good call on the unhooking. If I decide to distribute this one, I'll be sure to wrap up the functions into a class or something. Polluting the global namespace just makes me feel dirty. –  Kevin May 3 '12 at 11:54

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.