使用引用URL登录时有条件重定向

时间:2017-01-11 作者:Jen

我的客户网站有一个表单,只有具有特定用户角色(自定义)的人才能查看。他们正在发送一个指向RSVP页面的链接,但为了查看该页面,访问者必须在该站点注册或登录。

我遇到的问题是,如果他们必须注册(这个网站是新的,所以他们都必须注册),他们会丢失他们首先点击的引用链接,因为他们必须确认他们的电子邮件地址/设置密码。

客户端不希望访问者在登录/注册后进入配置文件/仪表板页面,因此问题是:

接收指向RSVP表单页面的链接>注册站点>设置密码>登录>转到RSVP表单页面。

这可能吗?如果是的话,有没有人能给我指出正确的方向,这样我就可以开始想办法了?

我尝试使用此代码,但登录后它仍会转到配置文件页面,因为我怀疑“引用”url在所有页面刷新中都丢失了。

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;

}
此外,如果他们只是登录而没有使用引用URL,他们应该直接访问主页(主页上有基于用户角色的有条件内容)。

1 个回复
最合适的回答,由SO网友:Kudratullah 整理而成

只需使用login_redirect 过滤并返回RSVP表单页面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 */);
    }
}

相关推荐

WP_REDIRECT导致无限循环

Goal: 我修改了permalink,以显示特定类别的类别名称:grill rezepte。默认情况下,永久链接为:https://website.com/post-title 现在只有烧烤餐厅:https://website.com/grill-rezepte/post-title因此,我想在所有grill Recipes上设置301重定向,以便它们移动到新的URL。我有下面的代码(见下文),它会导致一个无限循环。Code:add_action(\'template_redirect\', \'pos