WordPress:密码保护页面(2)

时间:2014-10-09 07:46:49

标签: php wordpress password-protection

我刚刚将我的网站升级到新版本WordPress 3.9.2。我注意到我的一个页面不像通常那样工作。此页面受密码保护,我对其外观进行了更改。当我升级时,它不再起作用了。在受密码保护的页面中,我有以下代码:

<?php
echo "<script type='text/javascript'>\nwindow.location = 'http://www.google.com'</script>";
?>

那个目的是重定向到另一个页面。它们与下面的代码齐头并进。 这是我的旧代码:

    <?php
function my_password_form() {
    global $post;
    $label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
    $o = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post">
    ' . __( "To view this protected post, enter the password below:" ) . '
    <label for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" maxlength="20" /><input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" />
    </form>
    ';
    return $o;
}
add_filter( 'the_password_form', 'my_password_form' );
?>

在升级之前,在输入密码后,它会将我重定向到另一个页面,这就是我希望它工作的方式。但请注意表单的action属性。在WP 3.9.2中,wp-pass.php不再存在,所以我在寻找另一个代码。我看到了这句话:

action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '"

但输入密码后,它会将我重定向到wp-login,这不是我想要的。我需要帮助,这与我使用的旧代码的工作方式相同。我不打算降级我的WP或安装任何插件。我只想改变action=""的值。谢谢!

2 个答案:

答案 0 :(得分:0)

我已经找到了答案。也许我的文件并不兼容,这就是为什么它没有用,但这里是完整的代码。

<?php
    function my_password_form() {
        global $post;
        $label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
        $o = '<form action="' . get_option('siteurl') . '/wp-login.php?action=postpass" method="post">
        ' . __( "To view this protected post, enter the password below:" ) . '
        <label for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" maxlength="20" /><input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" />
        </form>
        ';
        return $o;
    }
    add_filter( 'the_password_form', 'my_password_form' );
    ?>

注意: 我正在使用WordPress 3.9.2

答案 1 :(得分:0)

我有同样的问题,我找到了解决方案

1)使用密码

设置您的页面

2)将此表格插入另一页(典型的postpass wordpress表格):

<form action="https://exemple.com/wp-login.php?action=postpass" class="post-password-form" method="post" id="go-pro-espace">
     <input name="post_password" id="exemple" type="password" size="20" /><br />
     <input type="submit" value="submit">
</form>

3)更改/wp-login.php文件(根目录,此时位于第460行):

from:
wp_safe_redirect( wp_get_referer() );
to:
wp_safe_redirect( "https://exemple.fr/your-protected-page" );

看看答案:Wordpress protected page, POST form on a other page