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.

  • What if you hook it sooner ? Add a priority of 1 as such apply_filters( 'post_password_expires', 'custom_post_password_expires', 1 ); – Beee Feb 26 '18 at 0:04
  • Thanks much for the reply, but nope, adding the priority of 1 made no difference at all. – Zocios Feb 26 '18 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 Feb 26 '18 at 1:23
  • 1
    You should be using add_filter('post_password_expires', 'custom_post_password_expires'); not apply_filters('post_password_expires', ... – Dave Romsey Feb 26 '18 at 3:14
  • 1
    Good catch @DaveRomsey! :D – Sally CJ Feb 26 '18 at 6:55
0

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
}
  • Thank you! Works perfectly. A quick question so I understand this better: is the specific language 'wpse_custom_post_password_expires' important here? – Zocios Feb 26 '18 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. – Dave Romsey Feb 26 '18 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 Feb 26 '18 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. – Dave Romsey Feb 26 '18 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 Feb 26 '18 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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