将如下代码添加进主题的functions.php里面即可:

一、去除标题前的“密码保护:”等前缀


add_filter('protected_title_format', 'no_title_prefix');
add_filter('private_title_format', 'no_title_prefix');
function no_title_prefix( $prefix ) {
return '%s';
}


二、首页显示输入密码表单


function index_password_form($content) {
global $post;
if(post_password_required( $post )) {
return '<div>' . get_the_password_form() . '</div>';
}
return $content;
}
if(!is_singular()) {
add_filter('the_content','index_password_form');
}


三、自定义密码输入表单
如果觉得WP自动生成的表单不好看也不好用CSS渲染,想自行生成密码表单的HTML结构,把如下代码丢进functions.php里面可以做到:


function custom_password_form() {
global $post;
$label = 'pwbox-'.(empty($post->ID) ? rand() : $post->ID);
$o = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post">' . <strong>( "This post is password protected and this is what I want to say about that. To view it please enter your password below:" ) . '<label for="' . $label . '">' . </strong>( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" /><input type="submit" name="Submit" value="' . esc_attr__("Submit") . '" /></form>';
return $o;
}
add_filter('the_password_form', 'custom_password_form');


最后修改:2019 年 08 月 06 日
如果觉得我的文章对你有用,请随意赞赏