1

WordPress password protected posts or pages show by default the text "There is no excerpt because this is a protected post.", instead of the post excerpt.

The corresponding code is in wp-includes/post-template

if ( post_password_required( $post ) ) {
    return __( 'There is no excerpt because this is a protected post.' );

I am working on a Twentyseventeen child theme. The list of my posts (Blog) shows by default the post content before the 'read more tag'. I want exactly the same appearance for my password protected posts.

The WordPress codex has a solution how to alter the text:

There is no excerpt because this is a protected post

function my_excerpt_protected( $excerpt ) {
    if ( post_password_required() )
        $excerpt = '<em>[This is password-protected.]</em>';
    return $excerpt;
}
add_filter( 'the_excerpt', 'my_excerpt_protected' );

I did find this tutorial on WpBeginner, but :

a) I do not know what I do have to put for wpb_protected_excerpt_posts
b) I do not know if it is working at all or if a different solution would be more appropriate.

function my_excerpt_protected( $excerpt ) {
if ( post_password_required() ) {
$post = get_post();
$excerpt=$post->post_excerpt;
}
return $excerpt;
}
add_filter( 'the_excerpt', 'my_excerpt_protected' );

function wpb_protected_excerpt_posts( $content ) {
if ( post_password_required() && is_single() ) {
$post = get_post();

return $post->post_excerpt.$content;
}}
add_filter( 'the_content', 'wpb_protected_excerpt_posts', 10 );

Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.