functions.php if admin not visible
Removing the Admin Bar for Non-Admins
Removing the admin bar is a simple code snippet. I’ve shown before how you can hide the admin bar conditionally for users. In there I’ve not mentioned how you can hide it for everyone but admins, so thats where this code snippet comes in;
<?php // For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/
/**
* Hide admin bar for non-admins
*/
function js_hide_admin_bar( $show ) {
if ( ! current_user_can( 'administrator' ) ) {
return false;
}
return $show;
}
add_filter( 'show_admin_bar', 'js_hide_admin_bar' );
view rawhide-admin-bar-non-admin-wordpress.php hosted with ❤ by GitHub
Block Dashboard Access for Non-Admins
The best way to handle someone trying to access the dashboard is to redirect them to another page. You can consider redirecting them back to the primary landing page of your site, their front-end profile (if there’s any) or just redirect them back to the page they came from.
<?php // For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/
/**
* Block wp-admin access for non-admins
*/
function ace_block_wp_admin() {
if ( is_admin() && ! current_user_can( 'administrator' ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
wp_safe_redirect( home_url() );
exit;
}
}
add_action( 'admin_init', 'ace_block_wp_admin' );
view rawblock-dashboard-access-non-admins.php hosted with ❤ by GitHub
You can change the home_url() to something else if you want to redirect them somewhere else. For example, to redirect them to the shop page you can use home_url( '/shop' ) or if you have a profile page home_url( '/profile' ).
Redirecting users after login
A quick sample to redirect user after they’ve logged into your site;
<?php // For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/
/**
* Redirect users after login
*/
function js_login_redirect( $url ) {
return home_url( '/profile' );
}
add_filter( 'login_redirect', 'js_login_redirect' );
view rawlogin-redirect-wordpress.php hosted with ❤ by GitHub
Redirecting users after logout
Same as the above, you can also use it for the logout redirect;
<?php // For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/
/**
* Redirect users after logout
*/
function js_logout_redirect( $url ) {
return home_url( '/profile' );
}
add_filter( 'logout_redirect', 'js_logout_redirect' );
view rawlogout-redirect-url-wordpress.php hosted with ❤ by GitHub