让我的WordPress博客永远记住我的登录

时间:2011-03-14 作者:ripper234

有没有办法让Wordpress的“记住我”登录复选框可以工作很长时间,比如一年?目前我认为它最多只能工作几天或一周,通常我不记得了。

2 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

我想我会等待这样一个插件

将此代码保存在名为set-cookie-expire.php 并将其上载到您的插件文件夹。

该插件将使您能够在WordPress设置管理中设置Cookie过期的时间(以天为单位)。

<?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
    );
}

SO网友:Matt Chan

Wordpress 2.4以上版本存储两个cookie,根据Codex. 根据这个blog post (链接在Codex中),您可以编辑wp_set_auth_cookie 中的函数pluggable.php 中的文件wp-includes Wordpress文件夹。具体行取决于您使用的Wordpress版本。

更好的方法可能是编写或使用插件来替换wp_set_auth_cookie 功能。如果您编辑Wordpress核心文件,如果您使用自动更新程序或手动安装Wordpress并复制文件而不保留所做的任何更改,这些文件将被替换。

结束

相关推荐

Login form in popup

我将WP3与defaul主题一起使用。我希望人们在提交评论之前注册。我想做的是建立一个链接,而不是像“留下评论”这样的评论表单。单击此链接将打开一个弹出窗口,其中包含登录/注册表单或评论表单(如果用户已登录)。这可能吗?谢谢