avatar Deluxe Blog Tips About Projects

Howto Prevent Admin Panel Access For Logged In Users In WordPress

Sometimes you don't want normal users access WordPress admin panel, all users' actions can be done in the front-end. This article will show you a small tip for preventing admin panel access for logged in users in WordPress.

Paste the following code into your functions.php file:

add_action( 'init', function () {
    if ( false !== strpos( strtolower( $_SERVER['REQUEST_URI'] ), '/wp-admin' ) && !current_user_can( 'manage_options' ) ) {
        wp_redirect( home_url() );
    }
}, 0 );

The script will check the request URI, if it contains /wp-admin, and users don't have manage_options capability, then we redirect them to the homepage.

Preventing admin panel access is often used in many situations, where all users' actions can be done on the front end, for example submitting posts or editing user profile.

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com

Comments