Display Featured Images & Excerpts for Password Protected Content

Many WordPress themes hide the post excerpts and featured images 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 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”.

To display an actual post’s excerpt, you can choose one of the 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 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 code snippet into the functions.php file of your child theme based on what you want to show instead of the default text.

  • Replace the default WordPress excerpt 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 the default WordPress excerpt 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 the default WordPress excerpt 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 excerpts under Pages or custom post types, you need to add the Excerpt field to the page editor first. To do so, copy the following code and paste it into the functions.php file of your child’s 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 code snippet given below 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, the Hamilton theme hides the featured images 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 can 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:

Digiqole theme

Similar to the Hamilton theme, Digiqole doesn’t provide any hooks to modify their behavior. That means the featured images will be hidden until users enter a password by default.

To always show the featured images, remove the post_password_required() condition in line 415 as shown below.

It’s important to know that this modification will be lost after the theme update. Please contact the Digiqole team for further assistance.

Lasted updated on May 20, 2021

Leave a Reply

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