wordpress url redirect hook | PHP |

I am working on my wordpress website and i am little stuck into one situation.... i want to redirect the users to a specific page after successful login but i dont know how to use the wordpress redirect hook here below is my wordpress hook that i put in function file... code that i found online:-

/**
* WordPress function for redirecting users on login based on user role*/
function wpdocs_my_login_redirect( $url, $request, $user ) {

$urllinkin = 
if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
    if ( $user->has_cap( 'administrator' ) ) {
        $url = admin_url();
    } elseif() {
        $url = home_url( '/members-only/' );
    }
}
return $url;
}

add_filter( 'login_redirect', 'wpdocs_my_login_redirect', 10, 3 );

currently i am using this code in anchor tag to redirect to sign in page and after sign in to specific page, the sign in is going fine but redirect is not working

<?php echo wp_login_url(get_permalink()); ?>"> this code give the url :- http://192.168.1.50/jobifylocal/my-profile/?redirect_to=http://192.168.1.50/jobifylocal/job/clinical-psychologist/

hey i edited the code as per my need.. you commented, but still it dosent redirect

function wpdocs_my_login_redirect( $url, $request, $user ) { 
if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
if ( $user->has_cap( 'administrator' ) ) {
    $url = admin_url();

} elseif ( $user->has_cap( 'candidate' ) ) {
             $variable_two = $_GET['redirect_to'];
        if(!empty($variable_two)){
            $url =  $variable_two;
        }

   // $url = home_url( '/members-only/' );
}
}
return wp_redirect($url);
}
 add_filter( 'login_redirect', 'wpdocs_my_login_redirect', 10, 3 );

Answer

Solution:

$urllinkin = remove this from your code.

Further more here is the code for login redirect (Check official docs),

function my_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if ( in_array( 'administrator', $user->roles ) ) {
            // redirect them to the default place
            return $redirect_to;
        } else {
            return home_url();
        }
    } else {
        return $redirect_to;
    }
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

For redirection, you can use wp_redirect default function too.

Here is your updated code,

function wpdocs_my_login_redirect( $url, $request, $user ) { 
  if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
    if ( $user->has_cap( 'administrator' ) ) {
        $url = admin_url();
    } else {
        $url = home_url( '/members-only/' );
    }
  }
  return $url;
}
add_filter( 'login_redirect', 'wpdocs_my_login_redirect', 10, 3 );

Source