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.)


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' );
  • 3
    Just 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
  • This function work fine if you set role as a parametr. Feb 6 '18 at 20:38
  • Yeah, it "just work", but as the docs say: "this practice is discouraged as it may produce unreliable results". Better check roles property of WP_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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.