Ask your WordPress questions! Pay money and get answers fast! (more info)

You must type at least one more sentence WordPress

You must type at least one more sentence to explain your question. Your subject should be less than 65 characters. It is currently 66 characters. Please shorten it.Your subject should be less than 65 characters. It is currently 66 characters. Please shorten it.Your subject should be less than 65 characters. It is currently 66 characters. Please shorten it.Your subject should be less than 65 characters. It is currently 66 characters. Please shorten it.Your subject should be less than 65 characters. It is currently 66 characters. Please shorten it.Your subject should be less than 65 characters. It is currently 66 characters. Please shorten it.Your subject should be less than 65 characters. It is currently 66 characters. Please shorten it.Your subject should be less than 65 characters. It is currently 66 characters. Please shorten it.

Answers (3)

2014-03-02

Hariprasad Vijayan answers:

Hi Jihan,

Try this

function my_login_redirect( $redirect_to, $request, $user ){
global $user;
if( isset( $user->roles ) && is_array( $user->roles ) ) {
if( in_array( "vanarama_franchisee", $user->roles ) ) {
return 'http://www.uprint.me.uk/product-category/vanarama/';
} else {
return $redirect_to;
}
}
else {
return $redirect_to;
}
}
add_filter("login_redirect", "my_login_redirect", 10, 3);

Add the code to function.php and check.


Jihan Ahmed comments:

the above code will not work with woocommerce. We need to woocommerce login form http://dev13.jihan.me/my-account/
can u come on skype.

2014-03-03

Bob answers:

found chunk of code on internet May be it become helpful to you.
please notecode is not tested.

/**remove wooocommerce's own login processing to replace with our own to enable redeirection by role */
remove_action('init','woocommerce_process_login');
add_action('init','msci_woo_after_login_redirect');
/**the below is a copy of woocommerce_process_login with a couple of bits added */
function msci_woo_after_login_redirect() {
global $woocommerce;

if ( ! empty( $_POST['login'] ) ) {

$woocommerce->verify_nonce( 'login' );

try {
$creds = array();

if ( empty( $_POST['username'] ) )
throw new Exception( '<strong>' . __( 'Error', 'woocommerce' ) . ':</strong> ' . __( 'Username is required.', 'woocommerce' ) );
if ( empty( $_POST['password'] ) )
throw new Exception( '<strong>' . __( 'Error', 'woocommerce' ) . ':</strong> ' . __( 'Password is required.', 'woocommerce' ) );

if ( is_email( $_POST['username'] ) ) {
$user = get_user_by( 'email', $_POST['username'] );

if ( isset( $user->user_login ) )
$creds['user_login'] = $user->user_login;
else
throw new Exception( '<strong>' . __( 'Error', 'woocommerce' ) . ':</strong> ' . __( 'A user could not be found with this email address.', 'woocommerce' ) );
} else {
$creds['user_login'] = $_POST['username'];
}

$creds['user_password'] = $_POST['password'];
$creds['remember'] = true;
$secure_cookie = is_ssl() ? true : false;
$user = wp_signon( $creds, $secure_cookie );

if ( is_wp_error( $user ) ) {
throw new Exception( $user->get_error_message() );
} else {
/**********************************************************************************************
* added to original function to redirect depending on user roles]
***********************************************************************************************/
/*********************************************************************
* set your redirects depending on user role
* note: some users may have more than one role in which case you may want
* to run a foreach loop or something
* instead of using $roleRedirect[$user->roles[0]] below
***********************************************************************/
$roleRedirect['super_admin']='/wp-admin';
$roleRedirect['administrator']='/wp-admin';
$roleRedirect['vanarama_franchisee']='/product-category/vanarama/';
/**********************************************************************************************
* END->[added to original function to redirect depending on user roles]
**********************************************************************************************/

if ( ! empty( $_POST['redirect'] ) ) {
$redirect = esc_url( $_POST['redirect'] );
}

/**********************************************************************************************
* added to original function to redirect depending on user roles]
***********************************************************************************************/
elseif(isset($roleRedirect[$user->roles[0]])){
$redirect = esc_url($roleRedirect[$user->roles[0]]);
}
/**********************************************************************************************
* END->[added to original function to redirect depending on user roles]
**********************************************************************************************/
elseif ( wp_get_referer() ) {
$redirect = esc_url( wp_get_referer() );
} else {
$redirect = esc_url( get_permalink( woocommerce_get_page_id( 'myaccount' ) ) );
}

wp_redirect( apply_filters( 'woocommerce_login_redirect', $redirect, $user ) );
exit;
}
} catch (Exception $e) {
$woocommerce->add_error( $e->getMessage() );
}
}
}


Bob comments:

Here is some other logic might work.
code is not tested.

add_filter('woocommerce_login_redirect', 'ras_login_redirect');

function ras_login_redirect( $redirect_to ) {
$current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) )
return;
$roles = $current_user->roles; //$roles is an array

if( $user->roles[0] == "vanarama_franchisee" )
$redirect_to = 'http://www.uprint.me.uk/product-category/vanarama/';

return $redirect_to;
}


Bob comments:

Oh there was one mistake in above code.
here is new one

add_filter('woocommerce_login_redirect', 'ras_login_redirect');
function ras_login_redirect( $redirect_to ) {
$current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) )
return;
$roles = $current_user->roles; //$roles is an array

if( $roles[0] == "vanarama_franchisee" )

$redirect_to = 'http://www.uprint.me.uk/product-category/vanarama/';

return $redirect_to;

}


Bob comments:



add_filter('woocommerce_login_redirect', 'ras_login_redirect');
function ras_login_redirect( $redirect_to ) {

global $current_user;

if(in_array('vanarama_franchisee', $current_user->roles))
$redirect_to = 'http://www.uprint.me.uk/product-category/vanarama/';

return $redirect_to;
}

2014-03-03

Arnav Joy answers:

try this

add_filter('woocommerce_login_redirect', 'ras_login_redirect');

function ras_login_redirect( $redirect_to ) {

if( is_user_logged_in() ){
$current_user = wp_get_current_user();
if( $current_user->roles[0] == "vanarama_franchisee" )
$redirect_to = 'http://www.uprint.me.uk/product-category/vanarama/';
return $redirect_to;
}
return $redirect_to;
}