用户登录后重定向到目标URL

时间:2012-07-21 作者:Juan Lie

场景:如果用户未登录,则用户重定向到自定义登录页面,并在登录后再次重定向到目标页面。我不使用函数或插件。此代码到限制页:

if (!is_user_logged_in()){ wp_redirect( get_option(\'home\') . \'?redirect_to=\' . esc_url($_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]) );
}
成功登录到用户,但在登录页面中,URL更改为?page=登录,无法将URL重定向到目标页。如何制作?有人能帮忙吗?

3 个回复
SO网友:helgatheviking

我最近也做了类似的事情。

1. 如果用户未登录,我会捕获所需/目标页面的URL,并将其作为查询参数添加到登录页面的URL中。

2. 将用户重定向到登录页面。

function wpa_59205_redirect(){
    global $post;
    if ( ! is_user_logged_in() ) {
                // this will tack on the current page\'s url as a query arg for the login page\'s url
        $redirect = add_query_arg( \'redirect_to\', get_permalink( $post->ID ), $url_of_your_login_page_here );
                // redirect to the login page
        wp_redirect( $redirect );
        exit();
    }
}
add_action( \'template_redirect\', \'wpa_59205_redirect\' );
3. 然后,您可以使用login_redirect 滤器只需检查是否存在先前添加的查询变量:

function wpa_59205_login_redirect( $redirect_to ){
    if( isset( $_REQUEST[\'redirect_to\'] ) ) {
        return $_REQUEST[\'redirect_to\'];
    } else {
        return $redirect_to;
    }
}
add_filter( \'login_redirect\', \'wpa_59205_login_redirect\');
我使用WooCommerce登录页面进行了此操作,所以我过滤了他们专有的登录重定向过滤器,但是login_redirect 是相同的WP默认版本,因此我认为它应该可以工作,但尚未测试。

SO网友:chrisguitarguy

您的代码片段应该可以正常工作。

首先,您的自定义登录表单可能不尊重redirect_to. wp-login.php 是的,但你必须发送redirect_to 的参数wp-login.php 表单提交。显示登录表单的代码。

你需要小心使用wp_redirect. 它通过发送标题来工作:

<?php
/**
 * Redirects to another page.
 *
 * @since 1.5.1
 * @uses apply_filters() Calls \'wp_redirect\' hook on $location and $status.
 *
 * @param string $location The path to redirect to
 * @param int $status Status code to use
 * @return bool False if $location is not set
 */
function wp_redirect($location, $status = 302) {
    global $is_IIS;

    $location = apply_filters(\'wp_redirect\', $location, $status);
    $status = apply_filters(\'wp_redirect_status\', $status, $location);

    if ( !$location ) // allows the wp_redirect filter to cancel a redirect
        return false;

    $location = wp_sanitize_redirect($location);

    if ( !$is_IIS && php_sapi_name() != \'cgi-fcgi\' )
        status_header($status); // This causes problems on IIS and some FastCGI setups

    header("Location: $location", true, $status);
}
因此,如果部分页面已经加载,而您没有使用output buffering, 这行不通。Headers already sent 还有所有的生意。

如果是这样的话,您最好只显示一条带有登录表单链接的消息,或者显示表单本身(例如。wp_login_form).

其次,你应该随时打电话exitdie 使用后wp_redirect 这将导致PHP脚本(例如WordPress)的执行完成,并发送头和bail。否则,页面下方的内容可能会杀死重定向标头。

<?php
wp_redirect(site_url(\'wp-login.php\'));
exit;
最后,如果要在redirect_to URL您应该包括协议。

您也可以使用$_SERVER[\'REQUEST_URI\'].

<?php
$url = add_query_arg(\'redirect_to\', $_SERVER[\'REQUEST_URI\'], site_url(\'wp-login.php\'));
wp_redirect($url);
exit;

SO网友:AnupRaj

请注意:如果页面已启动,则不会调用wp\\u redirect,因此请确保向上调用它。

<?php 

    if (!is_user_logged_in()){
        $current_url = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
        wp_redirect( wp_login_url( $current_url ) ); 
        exit;
    }
?> 
wp\\U重定向:http://codex.wordpress.org/Function_Reference/wp_redirect

wp\\u登录\\u url:http://codex.wordpress.org/Function_Reference/wp_login_url

结束

相关推荐

wp-admin redirects to 404

昨天我登录了我的网站,发现当我以管理员身份登录时,出现了404错误,如下所示:我做了一些研究,上传了一个备份,修改了我的.htaccess 文件收件人:# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f Rewrite