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.
Leave a Reply