钩子文档

the_password_form

💡 云策文档标注

概述

the_password_form 是一个 WordPress 过滤器,用于修改受保护文章密码表单的 HTML 输出。它允许开发者自定义表单结构和样式,并提供了参数以处理密码验证和文章对象。

关键要点

  • 过滤器名称:the_password_form,用于过滤受保护文章密码表单的 HTML 输出。
  • 参数:$output(表单 HTML 输出)、$post(WP_Post 对象)、$invalid_password(无效密码消息)。
  • 注意事项:修改密码字段时,需注意 WordPress 数据库模式将密码字段限制为 255 个字符,不受 minlength 或 maxlength 属性影响。
  • 相关函数:get_the_password_form() 用于检索受保护文章密码表单内容。
  • 变更历史:6.8.0 版本添加 $invalid_password 参数,5.8.0 版本添加 $post 参数,2.7.0 版本引入。

代码示例

function wpdocs_custom_password_form() {
    global $post;

    $loginurl = site_url() . '/wp-login.php?action=postpass';
    $label = 'pwbox-' . ( ! empty( $post->ID ) ? $post->ID : rand() );

    ob_start();
    ?>

    <form action="<?php echo esc_url( $loginurl ); ?>" method="post" class="center-custom search-form" role="search">
        <label for="<?php echo esc_attr( $label ); ?>" class="post-password-label">
            <input id="<?php echo esc_attr( $label ); ?>" class="input post-password-class" type="text" placeholder="Enter your access code provided by your concierge" />
            <input type="submit" name="Submit" value="Submit" />
        </label>
    </form>

    <?php
    return ob_get_clean();
}

📄 原文内容

Filters the HTML output for the protected post password form.

Description

If modifying the password field, please note that the WordPress database schema limits the password field to 255 characters regardless of the value of the minlength or maxlength attributes or other validation that may be added to the input.

Parameters

$outputstring
The password form HTML output.
$postWP_Post
Post object.
$invalid_passwordstring
The invalid password message.

Source

return apply_filters( 'the_password_form', $output, $post, $invalid_password );

Changelog

Version Description
6.8.0 Added the $invalid_password parameter.
5.8.0 Added the $post parameter.
2.7.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    This is a working example and 9999 is the priority and you can adjust accordingly.

    function wpdocs_custom_password_form() {
    	global $post;
    
    	$loginurl = site_url() . '/wp-login.php?action=postpass';
    	$label = 'pwbox-' . ( ! empty( $post->ID ) ? $post->ID : rand() );
    
    	ob_start();
    	?>
    
    	
    		.center-custom { margin: 0; position: absolute; top: 50%; left: 50%; -ms-transform: translate( -50%, -50% ); transform: translate( -50%, -50% ); }
    		.post-password-label { color: #fff; }
    		.post-password-class { width: 100% !important; }
    		.post-password-class:focus { color: #fff !important; border-color: #fff !important; }
    		@media only screen and ( max-width: 600px ) {
    			.center-custom {
    				width: 85%;
    			}
    		}
    	
    	
    <form action="" method="post" class="center-custom search-form" role="search"> <label for="" class="post-password-label"> <input name="post_password" id="" class="input post-password-class" type="text" placeholder="Enter your access code provided by your concierge" /> <input type="submit" name="Submit" class="button" value="" />