Support » Developing with WordPress » Modify the third (context) parameter in a filter?

  • I understand that the following filter modifies the parameters of the authentication cookies, in fact I have managed to modify its value since it is the first parameter $cookie, but what I need is to modify the parameter $expiration that translates to Expires in the cookie.

    So how could I modify the third parameter $expiration given that only $cookie can be returned and the rest seem to be in context?

    add_filter( "auth_cookie", "modify_auth_cookie_defaults", 10, 5 );
    function modify_auth_cookie_defaults($cookie, $user_id, $expiration, $scheme, $token) { 
    $expiration = YEAR_IN_SECONDS
    return $expiration; 
    }
    • This topic was modified 2 weeks, 3 days ago by ashop59.
    • This topic was modified 2 weeks, 3 days ago by ashop59.
Viewing 3 replies - 1 through 3 (of 3 total)
  • No, you cannot simply change the value of one of the other parameters of a filter. In this case you even break the cookie itself with this change, because the return value must come from $cookie and be a valid cookie content.

    You can change the $expiration with this filter here: https://developer.wordpress.org/reference/hooks/auth_cookie_expiration/

    Thread Starter ashop59

    (@ashop59)

    For some reason the auth_cookie_expiration filter doesn’t work on my site… if someone has the same problem here I share the function I used where all the parameters can be customized:

    function set_auth_cookie() {
        if (is_user_logged_in()) {
            $user_id = get_current_user_id();
            $settingValue = $_COOKIE['wp-settings-' . $user_id];
            $settingTimeValue = $_COOKIE['wp-settings-time-' . $user_id];
            
            wp_clear_auth_cookie();
            
            $auth_cookie_name = SECURE_AUTH_COOKIE;
    		$scheme           = 'secure_auth';
           $expiration = time() + YEAR_IN_SECONDS;
           $manager = WP_Session_Tokens::get_instance( $user_id );
           $token   = $manager->create( $expiration );
           
          $logged_in_cookie = wp_generate_auth_cookie( $user_id, $expiration, 'logged_in', $token );
           	$auth_cookie      = wp_generate_auth_cookie( $user_id, $expiration, $scheme, $token );
          	$secure = is_ssl();
           $secure_logged_in_cookie = $secure && 'https' === parse_url( get_option( 'home' ), PHP_URL_SCHEME );
    
           setcookie( $auth_cookie_name, $auth_cookie, array('expires' => $expiration,'path' => PLUGINS_COOKIE_PATH,'secure' => $secure,'httponly' => true,'samesite' => 'Strict') );
    		setcookie( $auth_cookie_name, $auth_cookie, array('expires' => $expiration,'path' => ADMIN_COOKIE_PATH,'secure' => $secure,'httponly' => true,'samesite' => 'Strict') );
    		setcookie( LOGGED_IN_COOKIE, $logged_in_cookie, array('expires' => $expiration,'path' => COOKIEPATH,'secure' => $secure_logged_in_cookie,'httponly' => true,'samesite' => 'Strict') );
    		if ( COOKIEPATH != SITECOOKIEPATH ) {
    			setcookie( LOGGED_IN_COOKIE, $logged_in_cookie, array('expires' => $expiration,'path' => SITECOOKIEPATH,'secure' => $secure_logged_in_cookie,'httponly' => true,'samesite' => 'Strict') );
    		}
         setcookie( 'wp-settings-' . $user_id, $settingValue, array('expires' => $expiration,'path' => SITECOOKIEPATH,'secure' => $secure,'httponly' => true,'samesite' => 'Strict') );
    	setcookie( 'wp-settings-time-' . $user_id,$settingTimeValue, array('expires' => $expiration,'path' => SITECOOKIEPATH,'secure' => $secure,'httponly' => true,'samesite' => 'Strict') );
           
        }
    }
    add_action( 'init', 'set_auth_cookie' );
    Thread Starter ashop59

    (@ashop59)

    Thanks, I’m still working on it. I have some problem with the rest api in my site.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.