WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to make Wordpress' "Remember Me" login checkbox work for a really long time, like a year? Currently I think it only works for a few days or a week max, usually I am not remembered.

share|improve this question
up vote 9 down vote accepted

I think I'll wait for such a plugin

Save this code in a file named set-cookie-expire.php and upload it your plugins folder.

The plugin will give you the ability to set a time (in days) that cookies expire within the WordPress Settings admin.

<?php

/**
 * Plugin Name: Set Cookie Expire
 * Plugin URI:  http://wordpress.stackexchange.com/a/11991/1685
 * Version:     0.1
 * Description: Set the expire time for cookies in your <a href="options-general.php">general settings</a>.
 * Author:      WordPress Development Stack Exchange
 * Author URI:  http://wordpress.stackexchange.com/a/11991/1685
 */

/**
 * Set our user-specified expire times.
 *
 * @param  int $default
 * @param  int $user_id
 * @param  bool $remember
 * @return int
 */
function wpse_11979_set_cookie_expire( $default, $user_id, $remember ) {
    $days = get_option( $remember ? 'remember_cookie_expire' : 'normal_cookie_expire' );

    if ( $days === false )
        $days = $remember ? 14 : 2;
    else
        $days = ( int ) $days;

    return $days * DAY_IN_SECONDS;
}

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

/**
 * Register settings.
 */
function wpse_11979_set_cookie_expire_settings() {
    $settings = array(
        'normal_cookie_expire'   => 'Normal Cookie Expire',
        'remember_cookie_expire' => 'Remember Cookie Expire',
    );

    foreach ( $settings as $id => $label ) {
        add_settings_field(
            $id,
            $label,
            'wpse_11979_set_cookie_expire_field',
            'general',
            'default',
            array(
                'label_for' => $id,
            )
        );

        register_setting( 'general', $id, 'absint' );
    }
}

add_action( 'admin_init', 'wpse_11979_set_cookie_expire_settings' );

/**
 * Render settings field.
 *
 * @param array $args
 */
function wpse_11979_set_cookie_expire_field( $args ) {
    $id = $args['label_for'];

    printf(
        '<input id="%2$s" type="text" name="%2$s" value="%1$d" class="small-text" /> days',
        ( int ) get_option( $id, $id === 'normal_cookie_expire' ? 2 : 14 ),
        $id
    );
}
share|improve this answer
4  
Awesome! +10 for writing a plugin as an answer. – ripper234 Mar 14 '11 at 16:43

Wordpress versions great than 2.4 store two cookies that set to expire in two weeks according to the Codex. According to this blog post (linked in the Codex), you can edit the wp_set_auth_cookie function in the pluggable.php file in the wp-includes folder of Wordpress. The exact line will depend on which version of Wordpress you are using.

What is probably better is to write or use a plug-in to replace the wp_set_auth_cookie function instead. If you edit the Wordpress core files, they will be replaced if you use the auto-updater or do a manual installation of Wordpress and copy the files without preserving whatever changes you made.

share|improve this answer
    
Thanks for the answer - I think I'll wait for such a plugin, not too thrilled to get into WP code right now. – ripper234 Mar 14 '11 at 14:10
3  
Good choice. You shouldn't ever edit core files, whether you're thrilled about it or not. ;) – tnorthcutt Mar 14 '11 at 15:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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