Home > Net >  I want to redirect Subscribers and Wordpress Admin to another url
I want to redirect Subscribers and Wordpress Admin to another url

Time:09-27

Dear all,

I want to redirect Subscribers and Wordpress Admin to another url.

I have tried to do it with the following code but it does not work for me and I would like to improve it, or someone suggests a better coding.

I appreciate your efforts.

function custom_login_redirect($redirect_to, $request, $user) {
  global $user;

  if ( isset( $user->roles ) && is_array( $user->roles ) ) {
    if ( in_array( 'subscriber', $user->roles ) ) {
      return home_url("https://destodo.com/mi-escritorio/");
    }
  }

  return $redirect_to;
}
add_filter( 'login_redirect', 'custom_login_redirect', 10, 3 );

CodePudding user response:

This should do the trick. We filters the login redirect URL and redirect any users (including admin) to your redirect url.

<?php

add_filter( 'login_redirect', function ( $redirect_to, $requested_redirect_to, $user ) {

    if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
            
        $redirect_to = 'https://destodo.com/mi-escritorio/';
    
    };
    
    return $redirect_to;

}, 10, 3 );

CodePudding user response:

You can try this:

It is working to redirect Subscribers and WordPress Admin to another URL

function login_redirect_based_on_roles($user_login, $user)
{
    if (in_array('subscriber', $user->roles) || in_array('administrator', $user->roles)) {
        exit(wp_redirect('https://destodo.com/mi-escritorio/'));
    }
}

add_action('wp_login', 'login_redirect_based_on_roles', 10, 2);

CodePudding user response:

I think I have achieved it with the following modification to the codes that you sent, where the subscriber accesses a page and the administrator to the desktop.

    function login_redirect_based_on_roles($user_login, $user)
{
    if (in_array('subscriber', $user->roles) ) {
        exit(wp_redirect('https://pymecontable.com/mi-escritorio/'));
    }
}

add_action('wp_login', 'login_redirect_based_on_roles', 10, 2);
  • Related