Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Show Excerpt of a Password Protected Post in WordPress

Password-protecting your WordPress posts is a great way to keep specific content private.

However, there are times when you might want to share a teaser or excerpt to grab a visitor’s attention before asking them to enter the password.

The problem is that WordPress doesn’t make this easy by default. It completely hides the content of password-protected posts, which can make it harder to entice readers to unlock the full content.

After testing, we discovered that adding a custom code snippet can force excerpts to display for password-protected posts. The easiest way to set this up is with WPCode.

In this article, we’ll show you how to display excerpts for password-protected posts on your WordPress site.

Showing excerpt for password protected posts

Why Show the Excerpt of Password-Protected Posts?

Sometimes, completely hiding password-protected posts can feel like putting up a wall that could turn visitors away. If they can’t see what they’re missing, they might not feel motivated to unlock the content.

Showing an excerpt gives readers a sneak peek of what’s inside. It’s like sharing the first chapter of a book: it sparks curiosity and makes them want more.

Whether it’s premium content, an exclusive video, or a members-only guide, a preview can be just the nudge someone needs to sign up, log in, or enter a password.

This is especially helpful for businesses or creators who offer memberships. A well-crafted excerpt showcases the value of your content and strengthens trust with your audience by showing them exactly what they’re signing up for.

It’s also helpful in simpler cases, like sharing personal content with family and friends using password protection.

In the end, it’s about finding the balance between keeping your content secure and making it enticing enough for visitors to engage with it. Showing excerpts is a simple way to do just that.

Excerpts on a password-protected post

Do note that the ‘Add an excerpt…’ option is always available in the WordPress editor.

However, for password-protected posts, the excerpts won’t show up unless you add a custom code snippet to make them visible.

With that in mind, we’ll show you how to show excerpts of password-protected posts using a custom code plugin. Here’s a quick overview of all the steps we’ll cover in this guide:

Ready? Let’s get started!

Step 1: Instaling a WordPress Code Snippet Plugin

Typically, people will tell you to add a custom code snippet to your theme’sfunctions.php file or a site-specific WordPress plugin.

However, this method is not very beginner-friendly. One tiny mistake, and you risk breaking your WordPress site altogether.

This is why we always recommend using a code snippet plugin, and our favorite is WPCode. Across our partner sites, we use WPCode to add and manage custom code snippets, so we know firsthand how powerful yet easy-to-use it can be.

For details about the plugin, you can check out our WPCode review.

WPCode's homepage

To add custom code snippets, you can use the free version of WPCode. However, upgrading to WPCode Pro will unlock a lot more features, like viewing the full revision history for all your snippets and scheduling your code.

For this tutorial, we’ll use the free version of WPCode.

So, let’s install and activate the WPCode plugin. If you need help, then you can see our guide on how to install a WordPress plugin.

Upon activation, you can navigate to Code Snippet » + Add Snippet. Then, you’ll want to hover over the ‘Add Your Custom Code (New Snippet)’ and click the ‘+ Add Custom Snippet’ button.

Adding custom code in WPCode

On the next screen, you’ll need to select the code type for your custom code snippet.

You can click on ‘PHP Snippet.’

Select the PHP snippet option

This will take you to the WPCode code snippet editor.

From here, you can add the name for your snippet. This is for your reference only, but you might still want to use something clear so you can easily refer back to it later.

For example, we named ours ‘Show Excerpt of a Password Protected Post.’

Naming the custom snippet

With that done, you can move on to the next step.

Step 2: Adding a Custom Snippet for Showing Excerpts of a Password-Protected Post

In this step, you’ll need to copy and paste the following code snippet into the WPCode ‘Code Preview’ field:

function wpb_protected_excerpt($excerpt) {
    if (post_password_required()) {
        $post = get_post();
        if (!empty($post->post_excerpt)) {
            return '<p>' . esc_html($post->post_excerpt) . '</p>';
        }
    }
    return $excerpt;
}
add_filter('the_excerpt', 'wpb_protected_excerpt');

function wpb_protected_excerpt_posts($content) {
    if (post_password_required() && is_single()) {
        $post = get_post();
        if (!empty($post->post_excerpt)) {
            return '<p>' . esc_html($post->post_excerpt) . '</p>' . $content;
        }
    }
    return $content;
}
add_filter('the_content', 'wpb_protected_excerpt_posts', 10);

Here’s what it might look like in the interface:

Custom snippet in the code preview

Then, simply toggle the switch from ‘Inactive’ to ‘Active.’

After that, go ahead and click the ‘Save Snippet’ button.

Save and activate code snippet WPCode

Step 3: Inserting the Post Excerpt Using the Block Editor

Now that the custom code snippet is activated on your site, you can open a post and add your excerpts.

Before everything else, make sure your post is password-protected. If you haven’t done so, you can find the setting in the ‘Status’ section.

In our case, we’ll click the ‘Published’ status link.

Opening the post status

Then, simply check the ‘Password protected’ option.

After that, you can add the password to the respective field.

Setting up password for the password-protected post

Once you do that, you can close the popup by clicking anywhere in the content editor outside the popup.

The next step is adding the post excerpt. To make it easy, you can simply copy the opening lines of your blog post.

Tip: WordPress doesn’t set a hard limit for how many characters you can add in the ‘Excerpt’ field. However, the way excerpts show up on your site depends on your theme or widget settings – they might cut off longer excerpts to fit the design.

We recommend keeping excerpts to about 55 words. But it’s a good idea to check how your excerpts look on your site and tweak them to match your style!

You’ll then click the ‘Add an excerpt…’ link, and a popup window where you can add an excerpt will appear.

Adding post excerpts

After pasting your excerpts into the ‘Excerpt’ field, click anywhere outside the popup on the content editor.

You should now be able to see the post excerpt below the ‘Set featured image’ settings.

Excerpt added

If anything, you can always tweak your excerpt by clicking the ‘Edit excerpt’ link.

Finally, hit the ‘Update,’ ‘Save,’ or ‘Publish button to make the changes live.

Now, you can visit your website and see the excerpt for the password-protected post in WordPress.

Excerpts on a password-protected post

Bonus Tip: Selling Premium Content and Creating a Paywall

If you’re looking to monetize your content, then adding a paywall might be a better choice.

A paywall locks your content until readers take specific actions, such as paying for access. It’s an effective way to earn revenue, especially if you want to avoid running ads. You can charge for ongoing subscriptions or offer a one-time fee for lifetime access.

The post's excerpts and custom message that visitors will see if they aren't subscribed and logged in

With MemberPress, creating and managing paywalls is simple. It’s one of the best paywall plugins, and we use it ourselves to manage our free video site.

To learn more about the plugin, please see our MemberPress review.

MemberPress lets you set up different subscription packages and decide which content is accessible at each level. This flexibility lets you offer multiple membership tiers tailored to your audience’s needs.

For more details, you can check out our guide on how to create a paywall in WordPress.

Note: Paywalls aren’t always about money. For instance, The New York Times uses them to grow email lists by asking visitors to subscribe to a newsletter or create a free account. Meanwhile, The Washington Post offers trials or free articles before requiring payment.

We hope this article helped you show excerpts for password-protected posts in WordPress. Next, you may also want to see our guide on hiding password-protected posts from the WordPress loop or our expert list of useful tools to manage and grow your WordPress blog.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

6 CommentsLeave a Reply

  1. Jiří Vaněk

    This is a great and simple solution. I was looking for something within WordPress that would work exactly like Patreon, where I had premium content. It shows a snippet of the article, but the rest is accessible only after entering a password. This solution came in very handy.

  2. Kim

    Hello- I went to use your code to Manually Showing The Excerpt of a Protected Post.

    It works great up until you put the password in and go to read the protected post- it comes up blank! So the excerpt shows on the category page. I go to the post, put in the password to read the entire thing and it comes up blank, despite there being a written post in the dashboard.

    Would appreciate an edit to this almost perfect manual override for excerpts on protected content!

    • Jeff

      Apparently post_password_required returns FALSE if the password has been entered. Thus the original logic fails. This may not be quite correct either, but it’s working for me.

      if ( post_password_required() ) {
      $post = get_post();

      return $post->post_excerpt.$content;
      } else {
      return $content;
      }

  3. Lori

    what would the code be for a page? This causes all my pages to be hidden, only header and footer showing.

    • Stefan

      Hi Lori,

      I have the same problem. Have you found a solutions for this problem?

      Of course anybody else is also invited to share help and iformation! ;- )

  4. Adrienne

    Not sure if this works now. People think content should be free and accessible…. Maybe for downloads or donations, I think it would work.

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.