$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>admin - How to remove wp panel for users|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

admin - How to remove wp panel for users

matteradmin10PV0评论

Hello i have a question , i want to create the whole panel for users by myself and i dont wanna just hide the wordpress panel i dont wanna let users have access to wordpress panel, is that possible , because i know how to hide it:

add_action('after_setup_theme', 'remove_admin_bar');

function remove_admin_bar() {
    if (!current_user_can('administrator') && !is_admin()) {
        show_admin_bar(false);
    }
}

but if someone type site/wp-admin he still have access.

Hello i have a question , i want to create the whole panel for users by myself and i dont wanna just hide the wordpress panel i dont wanna let users have access to wordpress panel, is that possible , because i know how to hide it:

add_action('after_setup_theme', 'remove_admin_bar');

function remove_admin_bar() {
    if (!current_user_can('administrator') && !is_admin()) {
        show_admin_bar(false);
    }
}

but if someone type site/wp-admin he still have access.

Share Improve this question edited Jan 12, 2019 at 13:55 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jan 12, 2019 at 13:39 MariuszMariusz 234 bronze badges 1
  • Ahhh, good old WPbeginner with its code full of bad practices... – Krzysiek Dróżdż Commented Jan 12, 2019 at 13:55
Add a comment  | 

1 Answer 1

Reset to default 0

First of all, you should never use current_user_can function with roles. You should use only capabilities as params, as Codex clearly states:

While checking against particular roles in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results.

So, if we've dealt with that major issue...

There are 2 things you have to do, if you want to disable wp-admin for subscribers:

1. Hide Admin Bar

Admin bar is the dark bar displayed at the top of a page, when user is logged in.

You can hide it using this code:

function remove_admin_bar() {
    if ( is_user_logged_in() ) {
        $user = wp_get_current_user();
        if ( in_array('subscriber', $user->roles) ) {
              show_admin_bar(false);
        }
    }
}
add_action('after_setup_theme', 'remove_admin_bar');

2. Prevent wp-admin access

function restrict_wpadmin_access() {
    if ( ! defined('DOING_AJAX') || ! DOING_AJAX ) {
        $user = wp_get_current_user();

        if ( in_array('subscriber', $user->roles) ) {
            wp_redirect( site_url( '/user/' ) );  // <- or wherever you want users to go instead
            die;
        }
    }
}
add_action( 'admin_init', 'restrict_wpadmin_access' );

If you want to target different user group, all you need to do is to change this line in both functions above to match your needs:

if ( in_array('subscriber', $user->roles) ) {

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far