How to Add a Logout Button to Password Protected Posts in WordPress (2026)
WordPress password-protected posts have a hidden problem: there’s no logout. Once a visitor enters the password, a cookie keeps the post unlocked (for 10 days by default), so anyone using that browser later can read it. In 2014 we added a logout button to password protected posts on QuickOnlineTips with a small plugin. That plugin hasn’t been updated in years, so here’s a simple, current way to do it in 2026.

Quick answer: WordPress stores the post password in a cookie named wp-postpass_ plus a hash. To log visitors out, add a small code snippet that shows a “Log out” link on unlocked protected posts and deletes that cookie when clicked. Add it with a code snippets plugin or your child theme’s functions.php, and optionally shorten how long the password cookie lasts.
Table of Contents
Why password protected posts need a logout
- The unlock cookie survives closing the browser.
- On shared or family computers, the next person can open the post without the password.
- If you change the post password, existing cookies stop working, but that locks everyone out.
Add a logout link with a code snippet
Paste this into the free Code Snippets plugin or your child theme’s functions.php:
// Clear the post password cookie when ?postpass_logout=1 is visited
add_action( 'init', function () {
if ( isset( $_GET['postpass_logout'] ) ) {
setcookie( 'wp-postpass_' . COOKIEHASH, '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN );
wp_safe_redirect( remove_query_arg( 'postpass_logout' ) );
exit;
}
} );
// Show a logout link on unlocked password-protected posts
add_filter( 'the_content', function ( $content ) {
if ( is_singular() && post_password_required() === false && ! empty( get_post()->post_password ) ) {
$url = esc_url( add_query_arg( 'postpass_logout', '1' ) );
$content .= '<p><a class="btn-logout" href="' . $url . '">Log out of this protected post</a></p>';
}
return $content;
} );Test it on a staging site first, then open a protected post, enter the password and click the link: the password form should reappear.
Style the logout button
We used a large green button floating to the right. A modern version:
.btn-logout {
display: inline-block;
padding: 10px 18px;
background: #1a7f37;
color: #fff;
font-weight: 700;
border-radius: 8px;
text-decoration: none;
}If changes don’t appear, clear your cache or force a CSS refresh.
Shorten the password cookie
WordPress keeps the cookie for 10 days. To make it expire when the browser closes, add:
add_filter( 'post_password_expires', '__return_zero' );Plugin option
The Protected Posts Logout Button (external site) plugin we used in 2014 still adds a button automatically, but it hasn’t been tested with recent WordPress versions, so the snippet above is safer.
Better options for private content
Post passwords are shared secrets, not real security. For members-only content, use user accounts and a membership plugin; see our guide to turning a WordPress blog into a membership site.
FAQ
How long does a WordPress post password last?
The unlock cookie lasts 10 days by default, unless you change it with the post_password_expires filter.
Does logging out of WordPress clear post passwords?
No. The post password cookie is separate from your login.
Are password-protected posts secure?
They keep casual visitors out, but anyone with the password can share it. Use member accounts for sensitive content.

$4.99Free when you subscribe
Get 100 tech terms explained in plain English
8-page PDF on AI, coding, slang and SEO + one tech email a week.
Sent by email. Unsubscribe anytime. See our privacy policy.






Great tips ; Thanks for sharing this tips with us. Keep sharing and posting new tricks and tips for WordPress.
I’ve been browsing online from more time, yet I never found any interesting article like yours. It is pretty worth enough for me. In my view, if all webmasters and bloggers made good content blog and article as you did, the internet will be much more useful than ever before.
Yes, I agree with you. It is really important for my wordpress blog. I apply now for my site and thanks for your sharing.
Well, that was a serious issue that one faced in the case of accessing restricted content. Yes, the condition is much severe in the case of public computers. Users, if unable to log out, will carelessly close it and leave. But the cookies will ensure that the session can ce resumed upon re opening the browser!
The solution provided will ensure that the log out button is present at a prominent location, in front of the visitors’ sight. A handy guide indeed!