問題描述
有沒有辦法使 Wordpress 的”Remember Me” 登錄複選框工作了很長一段時間,像一年?目前我認為這隻能工作幾天或者一個星期,通常我不記得了。
最佳解決方案
I think I’ll wait for such a plugin
將此代碼保存在名為 set-cookie-expire.php 的文件中,並將其上傳到您的插件文件夾。
該插件可以讓您設置 Cookie 在 WordPress 設置管理員中過期的時間 (以天為單位) 。
<?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 版本超過 2.4 存儲兩個 cookie,根據 Codex 設置在兩週內到期。根據此 blog post(在 Codex 中鏈接),您可以在 WordPress 的 wp-includes 文件夾中的 pluggable.php 文件中編輯 wp_set_auth_cookie 功能。確切的行將取決於您正在使用的 Wordpress 版本。
最好是編寫或使用 plug-in 代替 wp_set_auth_cookie 功能。如果您編輯 Wordpress 核心文件,如果您使用 auto-updater 或手動安裝 Wordpress 並複製文件,而不保留您所做的任何更改,它們將被替換。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。