A few years ago, I added the following function (adapted from various code I found online) to customize the excerpt to display for password protected sites:
function get_the_custom_excerpt($excerpt) {
if ( post_password_required() ) {
$post = get_post();
$excerpt= '<p>' . $post->post_excerpt . '</p>';
}
if ( is_home() ) {
$post = get_post();
$excerpt= '<p>' . $post->post_excerpt . ' <a class="read-more" href="'. get_permalink($post->ID) . '"> Read More...</a> </p>';
}
else { $post = get_post();
$excerpt= '<p>' . $post->post_excerpt . '</p>';
}
return $excerpt;
}
add_filter( 'the_excerpt', 'get_the_custom_excerpt' );
Then added this code to make it work with Views:
remove_shortcode('wpv-post-excerpt');
add_shortcode('wpv-post-excerpt', 'get_the_custom_excerpt');
This has been working great to display excerpts on a View for a custom post type with password protected posts.
On a local staging version of the site, I upgraded Types and Views to the latest versions, and now the excerpts display as: "There is no excerpt because this is a protected post. "
I assume the remove_ and add_shortode code in my function is no longer working after the plugin update. Can you please advise how to fix this or why it broke after the update? I know I could just create a custom shortcode, but ideally, I'd prefer to get this working again if possible since I am using the customized excerpt shortcode throughout the site.
Thanks,
Sarah
Not sure offhand, can you tell me what event you are using to trigger this code? Or is it executed directly in functions.php without any hook listener?
remove_shortcode('wpv-post-excerpt');
add_shortcode('wpv-post-excerpt', 'get_the_custom_excerpt');
...vs...
function fix_shortcode(){
remove_shortcode('wpv-post-excerpt');
add_shortcode('wpv-post-excerpt', 'get_the_custom_excerpt');
}
add_action( 'init', 'fix_shortcode' );
It could be that the Toolset shortcode is now initialized later in the request lifecycle, and you need to use a different action hook to do your modifications.
Hi Christian,
I have the code directly in my functions.php. Just like this:
function get_the_custom_excerpt($excerpt) {
if ( post_password_required() ) {
$post = get_post();
$excerpt= '<p>' . $post->post_excerpt . '</p>';
}
if ( is_home() ) {
$post = get_post();
$excerpt= '<p>' . $post->post_excerpt . ' <a class="read-more" href="'. get_permalink($post->ID) . '"> Read More...</a> </p>';
}
else { $post = get_post();
$excerpt= '<p>' . $post->post_excerpt . '</p>';
}
return $excerpt;
}
add_filter( 'the_excerpt', 'get_the_custom_excerpt' );
// make settings work for [wpv-post-excerpt] shortcode of Views plugin
remove_shortcode('wpv-post-excerpt');
add_shortcode('wpv-post-excerpt', 'get_the_custom_excerpt');
so no action hooks to account for this issue.
After a bit more research, I found this on the WordPress Codex:
https://codex.wordpress.org/Using_Password_Protection
It appears that "There is no excerpt because this is a protected post." is the expected excerpt when the post is password-protected. The expected behavior is what you see, so it looks like this is working correctly now whereas before it was a bug.
Also I wanted to point out that the "else" block in your logic will be triggered if the post is password protected and not is_home. So be aware that modifications to the $excerpt value in the if ( post_password_required()) block may be overridden by the else block unless this code is executed on the home page:
function get_the_custom_excerpt($excerpt) {
if ( post_password_required() ) {
$post = get_post();
$excerpt= 'Protected: <p>' . $post->post_excerpt . '</p>';
}
if ( is_home() ) {
$post = get_post();
$excerpt= '<p>' . $post->post_excerpt . ' <a class="read-more" href="'. get_permalink($post->ID) . '"> Read More...</a> </p>';
}
else { $post = get_post();
$excerpt= 'Not home: <p>' . $post->post_excerpt . '</p>';
}
return $excerpt;
}
Hi Christian,
I realize that the "There is no excerpt" text is expected behavior for protected posts. That's why I added this code, specifically to counter the expected behavior since I need to display the excerpts, and this worked fine for several years until I updated the toolset plugins.
If I turn off the toolset Archive view where this displays and just use the default WordPress Archive, the excerpt display properly. So, I believe there is nothing wrong with the first part of my code. It is just the second part that applies to the Views shortcode where it is failing. I don't believe it was previously a bug.
If I remove the else block you mentioned and just have the post_password_required, the shortcode still doesn't work as before.
If I remove all the code except
remove_shortcode('wpv-post-excerpt');
the excerpt still displays. Shouldn't it not display at all if I remove_shortcode (or it should display [wpv-post-excerpt] )? So, this reinforces to me that there is something different with how Views handles this code since the update, and it has nothing to do with my preceding code.
-Sarah
Hello again,
I had some time to play around with the code and stumbled upon a solution.
If I replace the lines
remove_shortcode('wpv-post-excerpt');
add_shortcode('wpv-post-excerpt', 'get_the_custom_excerpt');
with what you provided in your example
function fix_shortcode(){
remove_shortcode('wpv-post-excerpt');
add_shortcode('wpv-post-excerpt', 'get_the_custom_excerpt');
}
add_action( 'init', 'fix_shortcode' );
it works again! (the excerpt displays, so the wpv-post-excerpt is being properly removed and added back with my custom coding applied)
Since my earlier test confirmed that the problems was the remove_shortcode was no longer working, it seems that after the latest Views update, remove_shortcode needs to be included in a function? I can't explain it, but it does work for me now, so thanks (the solution was in your first reply)! I'll keep this in mind if I run into similar difficulties as I update the rest of my sites.
-Sarah