投稿のパスワード保護を使う場合のカスタム方法
通常、公開状態設定でパスワード保護した場合でも保護対象は、投稿の抜粋the_excerpt()
と本文the_content()
とコメントcomments_template()
で表示される部分のみになり、カスタムフィールドが丸見え状態のため隠す方法です。
タイトルの[保護中:]を消す

function wpqw_remove_protected($title) {
return '%s';
}
add_filter('protected_title_format', 'wpqw_remove_protected');
パスワード保護された投稿の文字を変更する
抜粋の文字を変更する the_excerpt()


下記コードの3行目に好きなタグや文字を入れて変更します。
function wpqw_excerpt_protected( $excerpt ) {
if ( post_password_required() )
$excerpt = '<p>パスワードで保護されています</p>';
return $excerpt;
}
add_filter( 'the_excerpt', 'wpqw_excerpt_protected' );
記事一覧の抜粋にパスワード入力フォームを表示する方法

function wpqw_excerpt_password_form( $excerpt ) {
if ( post_password_required() )
$excerpt = get_the_password_form();
return $excerpt;
}
add_filter( 'the_excerpt', 'wpqw_excerpt_password_form' );
投稿本文の文字を変更する the_content()


下記コードの5行目に好きなタグや文字を入れて変更します。
function wpqw_password_form() {
global $post;
$label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
$o = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">
' . __( "<p>パスワードを入れてね</p>" ) . '
<label for="' . $label . '">' . __( "パスワード" ) . ' </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', 'wpqw_password_form' );
投稿ごとに文字を変更したい場合
投稿によってパスワード入力画面のメッセージを変えたい場合は、カスタムフィールドを使います。
カスタムフィールド部分をパスワード保護の対象にする場合
カスタムフィールドをパスワード保護の対象にするには、下記コードの2行目にカスタムフィールド出力用のコードを記述します。
<?php if ( !post_password_required( $post ) ) : ?>
// ここに書いた内容がパスワード保護の対象となります。
<?php endif; ?>
パスワード保護された投稿を一覧に表示しない
function wpqw_exclude_protected($where) {
global $wpdb;
return $where .= " AND {$wpdb->posts}.post_password = '' ";
}
function wpqw_exclude_protected_action($query) {
if( !is_single() && !is_page() && !is_admin() ) {
add_filter( 'posts_where', 'wpqw_exclude_protected' );
}
}
add_action('pre_get_posts', 'wpqw_exclude_protected_action');