My clients site has a form only viewable to people that have a specific user role (custom). They are sending out a link to an RSVP page but in order to view that page the visitor must be registered with the site or logged in.

The problem I'm running into is that if they have to register (which this site is new, so they all have to register), they lose the referring link they clicked on in the first place because they have to confirm their email address/set-up their password.

The client doesn't want the visitor to go to the profile/dashboard page after logging in/registering so the problem is this:

Receive link to page with RSVP form > register for site > set-up password > log in > go to page with RSVP form.

Is this even possible? If it is, can someone point me in the correct direction so I can start figuring out how to do this?

I tried using this code but it still goes to the Profile page after login because I suspect the "referring" url has been lost in all the page refreshes.

add_action( 'login_redirect', 'redirect_on_login' );

function redirect_on_login( $redirect_to ) {

    $homepage = get_site_url();

    // get whatever is stored in the GET-var 'login'
    $login = filter_input( INPUT_GET, 'login', FILTER_SANITIZE_STRIPPED );

    // if the content of 'login' is in the group (here as array), redirect to the homepage
if ( in_array( $login, array( 'incorrect', 'empty' ) ) ) {

    return $homepage;
} else {

// $_SERVER['HTTP_REFERER'] cannot really be trusted and $referrer can be empty.
// setup a default location for redirecting.
// @see: http://php.net/manual/en/reserved.variables.server.php
$referrer = ( isset( $_SERVER['HTTP_REFERER'] ) && ! empty( $_SERVER['HTTP_REFERER'] ) ) ?
$_SERVER['HTTP_REFERER'] : $homepage;

// wp-login.php use wp_safe_redirect, this means only pages on the same domain are accepted
// @see: http://codex.wordpress.org/Function_Reference/wp_safe_redirect

// get the host of our blog (strip scheme like http:// and https://)
$host = parse_url( $homepage, PHP_URL_HOST );

// check if the referrer is on the same host as the blog
$redirect_to = ( false != stristr( $referrer, $host ) ) ?
    $referrer : $homepage;

    return $redirect_to;


    // if everything fails, return the original value
    return $redirect_to;


Also, if they are just logging in and not using a referring URL, they should go directly to the homepage (which has conditional content based on user role).

1 Answer 1


just use the login_redirect filter and return RSVP form page permalink.

add_filter( "login_redirect", "redirect_on_login", 10, 3 );
function redirect_on_login( $redirect_to, $requested_redirect_to, $user ) {
    // check the user's role
    $role = $role = array_shift($user->roles);
    if( $role == "custom-role" ) {
        return get_permalink(/* post/page id of RSVP Form */);
  • Thanks, it's not exactly what I need though.
    – Jen
    Commented Jan 12, 2017 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.