6

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.

11

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
    );
}
3
  • 4
    Awesome! +10 for writing a plugin as an answer. – ripper234 Mar 14 '11 at 16:43
  • Sorry to answer here, but "you must have 50 points to comment...". @TheDeadMedic, plugin not works with current Wordpress version. – Eduard Dimitroff Apr 15 at 10:41
  • @EduardDimitroff note that you got automated flags against your account for doing that, be very careful as a 1 rep user you could be auto-flagged as a spammer. You can get 50 reputation easily by writing a good answer or a question that gets upvoted. Once you have 100 rep on any stack exchange site you get 100 on every other site ( including stackoverflow ). – Tom J Nowell Apr 15 at 11:12
2

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.

2
  • 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. ;) – Travis Northcutt Mar 14 '11 at 15:16

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.