1

I'm trying to change the default 10-day expiration for the cookie that allows a user to repeatedly view the content on a password-protected WP page without having to re-enter the page password during the default 10-day period. Rather than 10 days, I'd like to re-set the expiration to 30 seconds.

The WP code reference is here:

apply_filters( 'post_password_expires', int $expires )

This is what I have tried, without success:

function custom_post_password_expires() {
    return time() + 30; // Expire in 30 seconds
}
apply_filters('post_password_expires', 'custom_post_password_expires');

I've read the answers to previous similar questions, and none seem to apply or to provide a solution that works with the current version of WP. I suspect that the correct answer is very simple, but so far I'm not finding it. (Note: I am not an advanced developer, so I'd appreciate replies that are easy to understand :)

Thanks.

8
  • What if you hook it sooner ? Add a priority of 1 as such apply_filters( 'post_password_expires', 'custom_post_password_expires', 1 );
    – Beee
    Commented Feb 26, 2018 at 0:04
  • Thanks much for the reply, but nope, adding the priority of 1 made no difference at all.
    – Zocios
    Commented Feb 26, 2018 at 1:04
  • If increasing the priority doesn't work, then how about making it the lowest/last? E.g. The default priority is 10; now let's change it to 11 or any higher values. So, apply_filters('post_password_expires', 'custom_post_password_expires', 11);.
    – Sally CJ
    Commented Feb 26, 2018 at 1:23
  • 1
    You should be using add_filter('post_password_expires', 'custom_post_password_expires'); not apply_filters('post_password_expires', ... Commented Feb 26, 2018 at 3:14
  • 1
    Good catch @DaveRomsey! :D
    – Sally CJ
    Commented Feb 26, 2018 at 6:55

2 Answers 2

1

You should be using add_filter( ... ), not apply_filters( ... ):

/**
 * Filters the life span of the post password cookie.
 *
 * By default, the cookie expires 10 days from creation. To turn this
 * into a session cookie, return 0.
 *
 * @since 3.7.0
 *
 * @param int $expires The expiry time, as passed to setcookie().
 */
add_filter( 'post_password_expires', 'wpse_custom_post_password_expires' );
function wpse_custom_post_password_expires( $expires ) {
    return time() + 30; // Expire in 30 seconds
}
6
  • Thank you! Works perfectly. A quick question so I understand this better: is the specific language 'wpse_custom_post_password_expires' important here?
    – Zocios
    Commented Feb 26, 2018 at 17:18
  • You got it! wpse_ is just a prefix (abbreviation for WordPress StackExchange). It's a good practice to prefix all global functions and variables to avoid naming collisions. Commented Feb 26, 2018 at 19:22
  • I'm afraid I spoke too soon -- this works perfectly when I'm using Chrome or Firefox, but when I'm using Safari, it doesn't work at all :( Is there a fix for that?
    – Zocios
    Commented Feb 26, 2018 at 20:18
  • The code is not browser dependent. Use Safari's developer tools to delete the old cookie which probably the default one with a 10 day expiration. Commented Feb 26, 2018 at 20:20
  • Thanks again... actually, when I'd tried to use the Safari dev tools to delete the old cookie, nothing happened. But changing the password removed the old one, and now it works just like it should. Really appreciate the help! Problem solved, we can close this thread.
    – Zocios
    Commented Feb 26, 2018 at 20:49
0

I'm running Wordpress 5.7 now and indeed we live in the year 2021.

I'v got the same problem as Zocios.

A page is secured with a Password which you need to enter to view the page.

When you get out of that page and return again to that page, you can enter that page without having to enter the PW.

I added to coding to the functions.php page ( I hope as you can see in the picture here below)

And perhaps you can explain to me (I know a little of coding) how I can secure the page that when i return to the secured page I need to enter the PW each time I enter that page?

thanks for your help.

enter image description here

1
  • 1
    Welcome to WordPress Development! If you have a NEW question, please ask it by clicking the Ask Question button. If you have sufficient reputation, you may upvote the question. Alternatively, "star" it as a favorite and you will be notified of any new answers.
    – Glorfindel
    Commented Mar 11, 2021 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.