Last week, I posted a birth announcement card for our baby boy. In order to preserve his right to privacy, I set the visibility of the post to Password Protected. Resisting the temptation of “sharenting” (as it is called) was not a big deal; yet, given the circumstances surrounding my previous posts (e.g. When misfortune strikes, again!) I wanted to keep you updated with the situation. Therefore, I prepared an excerpt just for you:
In keeping with the previous post – One relief at a time… – I tested negative and could be with my wife in time. More importantly, mom and baby are both doing well. We are so happy and grateful that everything went well. Of course, our little boy is the most beautiful baby in the world, but you will have to trust his daddy on that matter. In order to preserve our baby’s privacy, this post will indeed be private; sorry, no photo to prove my statement.
In fact, I added a manual excerpt as I normally do (i.e. to be displayed in the Archives), but also copied the text directly in the actual post, just before a <More> tag. The idea was to increase the chances for you to see it by displaying it both in the Archives and Home page. Unfortunately, you could not see the excerpt (either way):
There is no excerpt because this is a protected post.
Of course, I should have known better. However, as alluded in my recent posts, I was not yet back to normal functioning and I didn’t read Using Password Protection (before to use this feature). So, how to show the excerpt of a Password Protected post?
Any quick and easy solution?
Although I could not – given my scarce time – look for a solution the way I would normally do (e.g. Comment form customization reloaded), as soon as I discovered the problem, I checked online for a quick fix (i.e. for someone else code).
The first result (Google search) was a WPBeginner article with a promising title1. However, I was a little reluctant to use the proposed code because of some comments (e.g. It works great up until you put the password in and go to read the protected post- it comes up blank!
). Therefore, I tried other approaches (see below) before this one. Anyway, not able to find anything better, I eventually came back to their code.
It’s actually two filters hooking into the_excerpt and the_content, respectively. I tried the first filter alone, but not to avail. When I added the second part of the code, things went wrong. As feared, I did lose the content from my Home Page, as well as every excerpts (i.e. not limited to this protected post) from my Archives! In the comment section of this WPBeginner article, Jeff shared a potential repair; implementing it helps somehow. It fixed the above bug, but also made the excerpt visible both in the Archives (i.e. manual excerpt) and in the Home Page (i.e. <More> tag).
Unfortunately, there were still some minor issues. The first was in the Archives: the excerpt was present, but immediately followed by There is no excerpt because this is a protected post.
– not making any sense, right? A similar thing occurred in the Home Page and the Single page, with the Password Form immediately after the excerpt. Although this was not a problem in the Home Page, the excerpt (i.e. the text before the <More> tag in this case) was not supposed2 to be displayed in the Single page. Now, before to try to dig deeper in this code, and maybe to fix these issues (live), let me go back in time a little bit…
And if you are wondering why I have time – now – to write this post, I am enjoying my 5-day paternal leave and baby is sleeping. I have three hours!
While this WPBeginner code was the first one I found, it was not the first I tried. Now, there were regrettably not many articles dealing with the excerpt not being displayed when using the Password Protected feature and, unfortunately, nothing (i.e. no solution/code) was working – at least not the way I wanted. Not mentioning those codes crashing my blog! Again, for lack of time, I had to give up (temporarily). Nonetheless, I would like to mention a not so bad workaround that may be of help to some of you. I found it in this stackoverflow question; the code proposed by manuel-84 was as follow:
function gettext_pp( $translation, $text ) {
if ( $text == 'There is no excerpt because this is a protected post.' ) {
$post = get_post();
$translation = $post->post_excerpt;
}
return $translation;
}
add_filter( 'gettext', 'gettext_pp', 10, 2 );
In short, this approach was not perfect, but was doing the job. Specifically, the code replaced the text There is no excerpt because this is a protected post.
with the actual excerpt. This was working only for the Archive page, though. Indeed, this could obviously not do the trick for my Home Page (where I used the <More> tag) because the text on that page was This post is password protected. To view it please enter your password below:
instead.
Time to take things in hand
As already explained, I did not implement immediately the second part of their code – the one hooking into the_content. Besides logic, the reason (why I didn’t use that filter) is that all the codes I came across, including those mentioned in the aforementioned WordPress.org article, were hooking into the_excerpt, not into the_content. Personally, I would have prefer to hook into get_the_excerpt instead, but apparently3, this is not possible.
Surprisingly, removing the first part of the WPBeginner code (the part with the_excerpt filter) did not change anything, suggesting that this filter was unworkable. To find out for sure, I tried two of the codes from the WordPress.org article – both hooking into the_excerpt. The first aimed at changing the text There is no excerpt because this is a protected post.
with your own. The second is expected to output the Password Form instead of the above text (in the Archives). As anticipated, neither worked! Of course, replacing the_excerpt (i.e. the name of the filter Hook) with the_content was enough to make both codes function (properly?). In fact, the first code (with the modified filter) output the replacement text in all three pages (i.e. Home Page, Single and Archive), making this option useless. The second one (also with the modified filter) displayed the Password Form everywhere as well (knowing it was already in Home Page and Single by default).
Why such a paradoxical solution?
The culprit is to be found in Twenty Seventeen code. Indeed, the people behind this theme decided to display full posts, instead of excerpts, in the Archives. In coding term, this means that they use the_content() instead of the_excerpt(). Why? I don’t know, but it is true that I had to correct this annoying behavior (back then4); this customization will actually be the topic of another post.
The <!–more–> quicktag requires templates to use the_content() whereas using excerpts requires, and allows, template writers to explicitly choose whether to display full posts (using the_content()) or excerpts (using the_excerpt()).
Bonus: my own solution!
Once the problem identified, it was relatively straightforward to come up with a temporary solution (at least for the Archives):
function my_excerpt_not_protected( $excerpt ) {
if ( post_password_required() && is_archive()) {
$post = get_post();
$excerpt = $post->post_excerpt;
}
return $excerpt;
}
add_filter( 'the_content', 'my_excerpt_not_protected' );
The code to display the excerpt on the Home Page (instead of having the Password Form) was a little trickier. Specifically, I wanted to have the excerpt (i.e. the text before the <More> tag), exactly as it would be displayed if the post was not Password Protected (or the way it is shown once the correct password has been entered). My solution might not be the most elegant; still, it works as desired:
function my_excerpt_not_protected( $excerpt ) {
if ( post_password_required() && is_archive()) {
$post = get_post();
$excerpt = $post->post_excerpt;
}
elseif ( post_password_required() && is_home()) {
$post = get_post();
$excerpt= '<p>' . $post->post_excerpt . ' <a class="more-link" href="'. get_permalink($post->ID) . "#more-{$post->ID}" . '" >Private</a> </p>';
}
return $excerpt;
}
add_filter( 'the_content', 'my_excerpt_not_protected' );
I may come back to this code in the future (when I will write the article about making Archives to use excerpts instead of full posts), but this is all I could come up with in my allotted time. Baby is waking up!
1 Syed Balkhi (2016) How to Show Excerpt of a Password Protected Post in WordPress. WPBeginner. ^
2 I mean “under normal conditions”. It is understood that WordPress is doing exactly what it is instructed to (in the code), that is to return the content immediately after the excerpt. ^
3 According to mor7ifer, in this stackoverflow question, it is not a pluggable function
. You can check this function in track right here and make your own opinion. ^
4 By the way, I didn’t recall that until I wrote this post! Allow me to put the blame on these trying times (see 100th post – a bittersweet celebration! and most of the posts published since). ^