This is feature image

How to Set WordPress Password Protected Page Cookie Timeout

Published by on March 8, 2024
Categories: BLOG WORDPRESS

Do you want to know how to set WordPress Password Protected Page Cookie Timeout?

WordPress password-protected pages restrict access to the page. The protected page has a default cookie length of 10 days. Using this feature, a user can view content on a password-protected WP page multiple times without entering the page password during the default 10-day period.

The issue lies in the cookie setting that expires after 10 days. After the client enters their password, anyone doesn’t have to enter it again through the same browser on the same machine for 10 days. The page content is accessible to anyone who will open this protected page on the same machine and browser. It poses a threat to the content’s security.

Setting a timeout on the cookie is the best approach for addressing this issue. This article will explain how to set a timeout for the cookie.

How to Set WordPress Password Protected Page Cookie Timeout

WordPress Password Protected Page Cookie Timeout

The best method to change the default cookie length of 10 days is by adding code. You can add code in functions.php or you can create a plugin to add code to change the default cookie.

We will use hook post_password_expires to set the cookie. By using the post_password_expires hook, it is possible to limit the cookie validity time to either a parameter value or the session itself.

Adding WordPress Password Protected Page Cookie Timeout Code to the functions.php file.

We have used WordPress post_password_expires.

add_filter( 'post_password_expires', 'wp_set_password_expire_limit' );

function wp_set_password_expire_limit( $expires ) {

    return time() + 60; // Expire in 60 seconds

}

To set the cookie to expire at the end of the session return 0;

Creating a Plugin to Add Code for  WordPress Password Protected Page Cookie Timeout.

<?php

/**

* Plugin Name: Set Expiry Time

* Plugin URI: https://www.it10x.com/

* Description: Time Limit For Expiring Password.

* Version: 0.1

* Author: Gstadevelper

* Author URI: https://www.gstadeveloper.com/

**/

add_filter( 'post_password_expires', 'wp_set_password_expire_limit' );

function wp_set_password_expire_limit( $expires ) {

    return time() + 60; // Expire in 60 seconds

}

?>

To set the cookie to expire at the end of the session return 0;

Conclusion

In WordPress, you have the option to change the default 10-day expiration for the cookie of the password-protected page by using a WordPress hook with custom code. In this tutorial, you have learned how to set a cookie timeout for WordPress password-protected pages. We hope this helps you change cookie timeout options.

Related Posts

gurjit singh

Gurjit Singh is a Content Writer and Web developer. He has experience in theme development, front-end development, and digital market. He loves to share his knowledge to help others in creating Websites, WordPress blogs, and SEO.