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 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 Post Type Protection option.

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

To show featured images for WordPress twenty nineteen theme for example, simply copy the following code and paste in functions.php file of your child theme.

add_filter('twentynineteen_can_show_post_thumbnail', function() {
     return ! is_attachment() && has_post_thumbnail();
});
Lasted updated on August 21, 2020

Leave a Reply

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