How and where, can i make function, that will redirect user (if it's not admin) to my specific URL? (the same - when logged user type /wp-admin URL.)
0
All functions place in functions.php
of your theme. And create redirect on template_redirect
hook.
add_action('template_redirect', 'make_redirect');
function make_redirect() {
if ( ! is_admin() && ! current_user_can( 'administrator' ) ) {
wp_redirect( 'https://google.com' );
}
}
-
3Just a hint:
current_user_can()
should not be used to check roles, it checks capabilities: "While checking against particular roles in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results." developer.wordpress.org/reference/functions/current_user_can – cybmeta Feb 6 '18 at 12:14 -
-
Yeah, it "just work", but as the docs say: "this practice is discouraged as it may produce unreliable results". Better check
roles
property ofWP_User
object. For exmaple,$user = wp_get_current_user()
and then `if( in_array( "administrator", (array) $user->roles ) – cybmeta Feb 6 '18 at 21:09