Display Featured Image & Excerpt for Password Protected Content

Many WordPress themes hide the post excerpt and featured image of password protected content by default. In this article, we will guide you on how to show these pieces of information to your visitors while still protecting the private content.

Requirement:

Show password protected post excerpt

Once a post is protected with a password, its excerpt will be hidden or replaced by WordPress default text – “There is no excerpt because this is a protected post”.

In order to display an actual post’s excerpt, you can choose one of these following options:

Force to show the post excerpt

If the theme you are using doesn’t show the post excerpt, simply enable our “Show Post Excerpt” option under the PPWP settings page.


Once enabled, your post excerpt will display on top of our password form.

If there are duplicate excerpts, it’s likely that your theme includes the default excerpt on their template as well. You should either turn off our setting option or remove the default excerpt on your (child) theme template.

Customize the default WordPress excerpt text

If your current theme already displays the default excerpt text, you can customize or even replace it with a password form.

Add the corresponding code snippet into the functions.php file of your child theme based on what you want to show instead of the default text.

  • Replace by the actual post excerpt
function show_post_excerpt( $excerpt ) {
   $post = get_post();
	if ( empty( $post ) ) {
		return '';
	}
	if ( post_password_required( $post ) ) {
		return $post->post_excerpt;
	}
	return $excerpt;
}
add_filter( 'the_excerpt', 'show_post_excerpt' );
  • Replace by the password form
function my_excerpt_protected( $excerpt ) {
    if ( post_password_required() && function_exists('ppw_core_render_login_form') ) {
        $excerpt = ppw_core_render_login_form();
}
    return $excerpt;
}
add_filter( 'the_excerpt', 'my_excerpt_protected' );
  • Replace by a custom text
function show_post_excerpt( $excerpt ) {
    if ( post_password_required() )
        $excerpt = 'Custom message';
    return $excerpt;
}
add_filter( 'the_excerpt', 'show_post_excerpt' );

Please note that where and how these excerpts display depends on your current theme.

An excerpt is assigned to Post by default. In case you want to display excerpt under Pages or custom post types, you need to add Exerpt field to page editor first. To do so, copy the following code and paste in functions.php file of your child theme.

add_post_type_support( 'page', 'excerpt' );

Then select the custom post types which you want to show excerpts under the Post Type Protection option.

Depending on the theme you’re using on your site, the featured image of a password protected post might be hidden by default.

Twenty Nineteen theme

To show the featured images of your protected posts, simply add the following code snippet to your (child) theme’s functions.php file.

add_filter('twentynineteen_can_show_post_thumbnail', function() {
     return ! is_attachment() && has_post_thumbnail();
});

Hamilton theme

By default, Hamilton theme hides the featured image until the password is entered. There’s no hook provided to modify this behavior, unfortunately.

However, you can resolve this by modifying the singular.php file in your child theme.

Simply remove the post_password_required() condition in line 48 so that the post thumbnails display, no matter if the content is protected or not.

If you want to show the post thumbnails in the blog post or home page, please copy and paste the content.php file to the child theme and edit it as below:

Lasted updated on March 5, 2021

Leave a Reply

Your email address will not be published. Required fields are marked *