Wordpress - Make my wordpress blog remember my login "forever"
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
);
}
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.