自定义登录表重定向到外部站点

时间:2016-05-16 作者:Justin Bamberg

我创建了一个私有页面,但该页面需要的不仅仅是为在可见性的发布设置中列出的私有页面输入代码,所以我创建了一个页面,并添加了一个if/else语句来检查用户是否登录。如果是这样,内容将按其应有的方式显示,如果不是,则会显示登录表单,并提示用户按其应有的方式登录。

问题是,他们使用表单登录后,会被重定向到管理页面。我已经添加了一个重定向到登录页面,以转到我想要的页面,但它似乎不起作用。我是否执行了错误的重定向?

<?php
/*
Template Name: Minutes
*/
?>

<?php get_header(); ?>

<?php get_template_part( \'templates/content\', \'nav\' ); ?>  
<?php get_template_part( \'templates/content\', \'breadcrumb\' ); ?> 




<section class="band bottom u-full-width" style="margin-top: 2%">
    <div class="container">
        <div class="row">


<?php if (is_user_logged_in()) { ?>


<div class="ten columns" >

<h5><b>Click the Plus (+) Sign to expend the list of available minutes per section</b></h5>   

Plus more code


<?php } else {
    echo \'We are sorry, but this area is only for registered, logged in users. If you are a registered user, please use the link below to login to the site to view content not available to the general public.  If you have any questions, please contact us.\';
    wp_login_form(array(\'redirect\' => \'http://etomv2.bambergmarketing.com/meeting-minutes\'));
}
?>

</div>

</div></div>
</section>

<?php get_footer(); ?>

1 个回复
SO网友:kaiser

当你看到wp_login_form(), 然后你会看到redirect 参数将要求提供绝对URl。所以要么使用admin_url()site_url() 如果要在内部链接。

这个action/ 此表单的目标不打算更改(除非您设置echoFALSE 使用一些\\DOMDocument 替换或某些正则表达式,以在输出字符串中对其进行更改):

action="\' . esc_url( site_url( \'wp-login.php\', \'login_post\' ) ) . \'" method="post"
当你看到wp_login.php and the case : \'login\'|default, 您将注意到两件事:

这里有a filter 调整重定向,这可能会覆盖参数中设置的任何内容。请记住,这甚至可能会根据执行登录操作的用户进行切换(通常用于将不同角色的用户重定向到不同的欢迎屏幕):

apply_filters( \'login_redirect\', $redirect_to, $requested_redirect_to, $user );
然后检查用户是否经过身份验证,而不是错误的对象。如果请求不是针对/wp-admin 另一方面,调用以下函数:

exit( wp_safe_redirect( $target ) );
…并使用wp_safe_redirect() 意味着你可以not 外部链接(从@TheDeadMedic评论中可以看出——谢谢!)。

解决方案与WordPress一样,您可以选择:

此函数位于pluggable.php, 也就是说你可以用你的own 作用这不是很稳定(每个其他插件都可以这么做),但如果你在个人网站上使用它,记录它并在发生冲突时修复它,也没关系。More info in this question 还有“ChipBennet”的回答

  • better solution 就是看wp_validate_redirect() 内部使用的wp_safe_redirect() 并利用WP提供的过滤器extend the list of allowed hosts.

    // End of `wp_validate_redirect()`
    $allowed_hosts = (array) apply_filters( 
        \'allowed_redirect_hosts\', 
        array( $wpp[\'host\'] ), 
        isset( $lp[\'host\'] ) ? $lp[\'host\'] : \'\' 
    );
    
    if ( 
        isset($lp[\'host\']) 
        && ( !in_array($lp[\'host\'], $allowed_hosts) 
        && $lp[\'host\'] != strtolower($wpp[\'host\'])) 
    )
        $location = $default;
    
    在您的情况下,允许重定向的(mu-)插件如下所示:

    <?php /* Plugin Name: Allow external login redirect */
    add_filter( \'allowed_redirect_hosts\', function( Array $hosts, $check )
    {
        return $hosts + [ \'http://etomv2.bambergmarketing.com\' ];
    }, 10, 2 );
    
    再看看里面的第二个参数wp_safe_redirect( $target, $fallback ) 用于wp_validate_redirect(). 设置为过滤器。这意味着,如果登录失败,您可以重定向到另一个外部URl。或者,您可以在本地重定向到某个错误页面(或仅重定向到当前页面,这是默认页面):

    apply_filters( \'wp_safe_redirect_fallback\', admin_url(), $status )
    
    第二种解决方案是我将使用的。您可能还希望在使用需要验证用户的某个外部OAuth提供程序时使用它。

    请注意,如果您阅读了文档并对login_post 作为其中的“方案”:site_url() 函数是的包装器get_site_url() 其中使用set_url_scheme() 内部。那里login_post, loginrpc 等于admin, 正确调整动作:

    $scheme = is_ssl() || force_ssl_admin() ? \'https\' : \'http\';
    

  • 相关推荐

    Redirect Css files to https

    在哪里可以找到负责调用Css文件的文件,并测试它们的链接是http还是https?我想测试请求是否为https,如果是,那么很好,如果不是,请将其更改为https在函数中。php我找到了负责调用主题Css的函数,下面是它的代码:if (!function_exists(\'jobcareer_front_scripts\')) { function jobcareer_front_scripts() { global $jobcareer_options;