5

This has got to be simple: how do I keep one user - my-user - logged in for a year? (I'm checking logged in cookies in the dev tools console).

Update 8/16/15

This works:

add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for_1_year', 10, 3 );

function keep_me_logged_in_for_1_year( $ttl, $user_id, $remember ) {
    if( 1 === $user_id )
        $ttl = YEAR_IN_SECONDS;
   return $ttl;
}

And with the array option, this works for multiple users:

add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for_1_year', 10, 3 );

function keep_me_logged_in_for_1_year( $ttl, $user_id, $remember ) {
    if( in_array( $user_id, array( 1, 2 ) ) )
        $ttl = YEAR_IN_SECONDS;
   return $ttl;
}

Update 8/12/15: Re: Rarst's answer below: since get_currentuserinfo is pluggable, how would I use apply_filters in this context?

I'm getting a Call to undefined function get_currentuserinfo() error with this function below used in a simple plugin:

global $user_login;
   get_currentuserinfo();
   if ($user_login == "my-user") {

function keep_me_logged_in_for_1_year( $expirein ) {
   return 31556926; // 1 year in seconds
}
add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for_1_year' );

}

But the Codex usage uses the global: https://codex.wordpress.org/Function_Reference/get_currentuserinfo

This, of course, keeps all users logged in for a year:

function keep_me_logged_in_for_1_year( $expirein ) {
   return 31556926; // 1 year in seconds
}
add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for_1_year' );
  • I think @Rarst has already solved it (therefore already upvoted it) and it looks like you're almost there with your keep_me_logged_in_for_1_year filter callback. You don't have to use apply_filters, just adjust your filter callback with the three input arguments: $ttl, $user_id and $remember. Then you can add the logic inside, the callback. The YEAR_IN_SECONDS constant might come handy here as well. – birgire Aug 14 '15 at 16:06
  • @birgire, thanks, I'm not clear on how to do the two methods - either apply_filters or add_filter - and added a bounty to ask for a full, working example for myself and others. – markratledge Aug 14 '15 at 16:36
4
+50

get_currentuserinfo() is a pluggable function, it is not available during plugins load stage.

That aside you shouldn't be adding filter conditionally, but use data provided by the filter. If you take a look at filter calls:

apply_filters( 'auth_cookie_expiration', 14 * DAY_IN_SECONDS, $user_id, $remember )

$user_id is provided as second argument. You just have your filter listen for it and modify return conditionally on it.

Here's an untested example:

add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for_1_year', 10, 3 );

function keep_me_logged_in_for_1_year( $ttl, $user_id, $remember ) {
    if( 123 === $user_id )
        $ttl = YEAR_IN_SECONDS;
   return $ttl;
}
  • Thanks! Let me take a look a this; I'm not sure how to use apply_filters in that context. – markratledge Aug 12 '15 at 16:24
  • @birgire: Thanks! Works fine. I wasn't having any luck using arguments in the filter; something I need to learn. One more question: why doesn't an array work? Like if (in_array(1,10) === $user_id ) ? Because of the arguments? – markratledge Aug 15 '15 at 21:58
  • super - did you try if( in_array( $user_id, array( 1, 10 ) ) ) ? @markratledge – birgire Aug 16 '15 at 12:28
  • Thanks again! That way of using of in_array works fine. – markratledge Aug 17 '15 at 1:44

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.