I am trying to build a class to make the job off adding new settings easier. The trouble I am having is that, although I have traced the variable through all of it’s stages, the string ‘manage_options’ doesn’t seem to grant admin the right to amend and option. I keep getting “You do not have sufficient permissions to access this page.” when I try access the new settings page.
Here is a heavily simplified version of the class, the creation function and its action hook.
class optionObject{
var $user_level = 'manage_options';
function add_page() {
add_options_page(menu_page_title, page_title, $this->user_level, menu_slug, array(&$this, 'do_page'));
}
function do_page(){
//do stuff to display page
}
}
function test_options(){
$options = new optionObject();
add_action('admin_menu', $options->add_page());
}
add_action('admin_init', 'test_options' );
Unedited Version here
admin_init
is called afterwp-admin/menu.php
is included, so the access check has already been executed and theadmin_menu
action has fired by the time you executetest_options()
. Remove theadmin_init
hook and calltest_options()
directly, or find another way of structuring your code so that theadmin_menu
hook is set up correctly.You might think it could work because you see the menu option when you are on other pages. This is because the menu is drawn after the page access is checked:
The menu is drawn in:
wp-admin/menu-header.php
on line 169wp-admin/admin-header.php
on line 143wp-admin/admin.php
line 132admin_init
fires inwp-admin/admin.php
on line 98The access check however is done in:
wp-admin/menu.php
on line 441admin_menu
fires inwp-admin/menu.php
on line 328wp-admin/admin.php
on line 93You see that adding menu items in
admin_init
is OK to be included in the drawn menu, but too late for the access check. That’s your current situation, and you need to change that by adding the page in theadmin_menu
hook or earlier.Jan is spot on with both his answer and comment on the original question..
Here’s an example of the code working, in it’s most basic form…
Hope that helps…