add_menu_page permissions – what am I doing wrong?

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:

Read More
<?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?

Related posts

Leave a Reply

2 comments

  1. 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!';
        }
    
  2. You have to use admin_menu rather than admin_init, that’s why you are getting error.

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

    You can also use anonymous function but organize your functions name is more cleaner way.