I’m trying to redirect the user to the last visited page, if that page was on the site domain. The code below correctly returns to true/false depending on the previous URL on the login page… However, it does not work using the gform_user_registration_login_redirect_url - probably because $_SERVER[‘HTTP_REFERER’] returns NULL, when the function runs.
Do you have any suggestions, so I can redirect users to the last visited page?
add_filter( 'gform_user_registration_login_redirect_url', 'gf_redirect_user_role', 10, 2 );
function gf_redirect_user_role( $login_redirect, $sign_on ) {
GFCommon::log_debug( __METHOD__ . '(): Running...' );
if ( ! in_array( 'administrator', $sign_on->roles ) ){
// get last page url
$last_page = $_SERVER['HTTP_REFERER'];
$profile_url = get_permalink(71);
// IF LAST PAGE WAS FROM KC-NITRA, redirect to last page. Else if go to profile, else go to homepage
if ( $last_page ) {
if ( ( strpos($last_page, 'kc-nitra') !== false ) or strpos($last_page, 'kreativnecentrumnitra') !== false ) {
$login_redirect = $last_page;
}
} else if ($profile_url) {
$login_redirect = get_permalink(71);
} else {
$login_redirect = get_home_url();
}
GFCommon::log_debug( __METHOD__ . '(): Redirecting to ' . $login_redirect );
}
return $login_redirect;
}