Custom get_the_password_form

时间:2019-01-19 作者:Forrest Walker Whitmore

我对为具有密码保护且具有特定类别的页面创建过滤器很感兴趣。我已将此代码放置在functions中主题的子文件夹中。php,因为我不想编辑wp包含文件。

/**
 * Retrieve v1-i1 protected post password form content.
 *
 * @since 1.0.0
 *
 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
 * @return string HTML content for password form for password protected post.
 */
function get_the_v1i1_password_form( $post = 0 ) {
    $post = get_post( $post );
    $label = \'pwbox-\' . ( empty($post->ID) ? rand() : $post->ID );
    $output = \'<form action="\' . esc_url( site_url( \'wp-login.php?action=postpass\', \'login_post\' ) ) . \'" class="post-password-form" method="post">
    <p>\' . __( \'This is exclusive content from v1:i1. Please enter password below:\' ) . \'</p>
    <p><label for="\' . $label . \'">\' . __( \'Password:\' ) . \' <input name="post_password" id="\' . $label . \'" type="password" size="20" /></label> <input type="submit" name="Submit" value="\' . esc_attr_x( \'Enter\', \'post password form\' ) . \'" /></p></form>
    \';

    /**
     * Filters the HTML output for the protected post password form.
     *
     * If modifying the password field, please note that the core database schema
     * limits the password field to 20 characters regardless of the value of the
     * size attribute in the form input.
     *
     * @since 2.7.0
     *
     * @param string $output The password form HTML output.
     */
    return apply_filters( \'the_password_form\', $output );
}


// If post password required and it doesn\'t match the cookie.
    if (post_password_required( $post ) && in_category( \'v1-i1\' )) {
    return get_the_v1i1_password_form( $post );
} else {
    return get_the_password_form( $post );
}
    `
我似乎没法让这件事起作用。我错过什么了吗?

2 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

如果你看看source codeget_the_content() 函数检索帖子内容,您将看到以下条件:

if ( post_password_required( $post ) )
    return get_the_password_form( $post );
get_the_password_form() 函数中,有一个过滤器可用于自定义默认密码表单的HTML:

return apply_filters( \'the_password_form\', $output );
因此,根据您的代码,它应该是这样的,并放置在主题的functions.php 文件:

function get_the_v1i1_password_form( $output ) {
    // If not in the v1-i1 category, return the $output as-is.
    if ( ! in_category( \'v1-i1\' ) ) {
        return $output;
    }

    // Here you can customize the form HTML.
    $post = get_post();
    $label = \'pwbox-\' . ( empty($post->ID) ? rand() : $post->ID );
    $output = \'<form action="\' . esc_url( site_url( \'wp-login.php?action=postpass\', \'login_post\' ) ) . \'" class="post-password-form" method="post">
    <p>\' . __( \'This is exclusive content from v1:i1. Please enter password below:\' ) . \'</p>
    <p><label for="\' . $label . \'">\' . __( \'Password:\' ) . \' <input name="post_password" id="\' . $label . \'" type="password" size="20" /></label> <input type="submit" name="Submit" value="\' . esc_attr_x( \'Enter\', \'post password form\' ) . \'" /></p></form>
    \';

    return $output;
}
add_filter( \'the_password_form\', \'get_the_v1i1_password_form\' );
注:该get_the_v1i1_password_form() 现在是筛选器回调,因此不应使用return apply_filters( \'the_password_form\', $output ); 这将导致页面停止工作。

你也不需要这个:

if (post_password_required( $post ) && in_category( \'v1-i1\' )) {
    return get_the_v1i1_password_form( $post );
} else {
    return get_the_password_form( $post );
}
附加说明以上代码可让您完全控制表单HTML,但如果您只想替换文本”This content is password protected. To view it please enter your password below:,那么您只需使用str_replace() 像这样:

function get_the_v1i1_password_form( $output ) {
    // If is in the v1-i1 category, replace the text.
    if ( in_category( \'v1-i1\' ) ) {
        return str_replace(
            \'This content is password protected. To view it please enter your password below:\',
            \'This is exclusive content from v1:i1. Please enter password below:\',
            $output
        );
    }

    return $output;
}
add_filter( \'the_password_form\', \'get_the_v1i1_password_form\' );
但当然,这只适用于get_the_password_form() 作用

SO网友:Mohsin Ghouri

您没有从函数返回任何内容get_the_special_password_form 如果您还需要标签,请返回上述函数的输出,并在返回值之前联系$标签到$输出。

相关推荐

change user password REST API

我试图让用户通过API更改密码。这里的情况是,我可以向用户端点发送一个POST请求,并在最后使用用户ID,在请求正文中以JSON的形式发送新密码。所以发布到:https://example.com/wp-json/wp/v2/users/123在身体里:{ "password": "mySecretPassword" } 在这种情况下,用户通过JWT进行身份验证,需要在请求的标头中发送令牌。当我在《邮递员》中尝试这一点时,请求挂起了