I am using below written code in my themes function.php file of WordPress site to restrict access to wp-admin and wp-login pages. and it's working very fine upon accessing /wp-admin and /wp-login and it's redirecting to my site url if user is not logged in or not administrator. but when i visit https://ghardwar.com/wp-login.php its not redirecting at all. Can anyone suggest me that what should i do to achieve this. Also is it possible to redirect logged-in non admins to some other page instead of home page retaining that non logged in user redirect function simultaneously.
/**
* Disable admin bar on the frontend of your website
* for subscribers.
*/
function wpestate_disable_admin_bar() {
if ( is_admin() && ! current_user_can( 'administrator' ) &&
! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
add_filter('show_admin_bar', '__return_false');
}
}
add_action( 'after_setup_theme', 'wpestate_disable_admin_bar' );
/*
* Restrict non logged users to certain pages
*/
add_action( 'init', 'blockusers_init' );
function blockusers_init() {
if ( is_admin() && ! current_user_can( 'administrator' ) &&
! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
wp_redirect( home_url() );
exit;
}
}
Use the plugin from the following link : https://wordpress.org/plugins/remove-dashboard-access-for-non-admins/
I think this might help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.