0

I can't find the answer for my question anywhere.

I am trying to make a login redirect script, after login the user (depending on their role) will go to a certain page. I have currently a working snippet in the functions.php - this works perfectly called from wp-login.php. However i have made a modal popup using AJAX, for the life of me can't get the AJAX to call the function in my functions.php

functions.php snippet

function admin_login_redirect( $url, $request, $user ){
    //is there a user
    if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
        //is user admin
        if( $user->has_cap( 'administrator' ) ) {
            //go do admin stuff
            $url = admin_url();
            //but wait there's more
        }
    }
    return $url;
}
add_filter('login_redirect', 'admin_login_redirect', 10, 3 );

function affiliate_login_redirect( $url, $request, $user ){
    if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
        if( $user->has_cap('yith_affiliate') && strpos($_REQUEST['redirect_to'], 'gf_entries') == false )  {
            //please god work
            $url = home_url() . '/affiliate-dashboard/';
            //but waittt there's more
        } else {
            //damnit all
            if( $user->has_cap('yith_affiliate') && isset($_REQUEST['redirect_to']) && strpos($_REQUEST['redirect_to'], 'gf_entries') !== false) {

            $url = $_REQUEST['redirect_to'];

            }
        }
    }
    return $url;
}
add_filter('login_redirect', 'affiliate_login_redirect', 10, 3 );

Now my AJAX does work to a degree, for testing i have routed it to google.com, this currently sends the ALL users to google.

AJAX snippet

if ( LRM_Pro.redirect_urls.after_login && "login" == action ) {
    window.location.href = "https://www.google.com";

So what i'm trying to get is that AJAX window.location.href to call my login redirect function to handle all the redirects based on the user cap (role).

Any help would be appreciated!

0

So, i managed to achieve what i want by directing the exisiting window.location.href to a newly created php file i put in the root called .login.php. I will share the code below as it handles the user roles nicely when logging in using this jquery method

.login.php

<?php
require_once('wp-load.php');

function user_role_check_modal( $role, $user_id = null ) {

  $user = wp_get_current_user();

    if ( empty( $user ) )
    return false;

  return in_array( $role, (array) $user->roles );
}

if ( user_role_check_modal( 'administrator' )):
   header("Location: /wp-admin/");
   exit();

endif;

if ( user_role_check_modal( 'customer' )):
   header("Location: /my-account/");
   exit();

else:

   header("Location: /my-account/");
   exit();

endif;

Hope this helps someone who maybe in the future needs work around

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.