0

How can i get add_action to work in a function, I want to hide a few admin menu item but not make the user and editor.

function my_remove_menu_pages() {
    remove_menu_page('tools.php');
    remove_menu_page('themes.php');
    remove_menu_page('options-general.php');
    remove_menu_page('plugins.php');
}

add_action('admin_init', 'check_username');
function check_username()
{
    $user = wp_get_current_user();

    if($user && isset($user->user_email) && 'username@example.com' == $user->user_email)
    {
        add_action( 'admin_menu', 'my_remove_menu_pages' );
    }
}
0

You can simply remove those menu items in your check_username method.

add_action('admin_init', 'check_username');
function check_username()
{
    $user = wp_get_current_user();
    if( $user && isset($user->user_email) && 'username@example.com' == $user->user_email ) {
        remove_menu_page('tools.php');
        remove_menu_page('themes.php');
        remove_menu_page('options-general.php');
        remove_menu_page('plugins.php');
    }
}

Your code doesn't work, because the admin_init action is invoked after admin_menu. Please see the Action reference page in WordPress Codex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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