检查用户是否在一个多月前注册

时间:2014-05-07 作者:pereyra

我该如何检查当前用户的注册时间是否超过(比如)30天前?我想只向该用户组显示某些内容。

非常感谢您的帮助。

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

您可以尝试以下操作:

/**
 * Is the current user registred for more than x days ago?
 * 
 * Based on the compare idea in this answer:
 * http://stackoverflow.com/a/7130744/2078474
 *
 * @link   http://wordpress.stackexchange.com/a/143597/26350
 * @param  int  $reg_days_ago
 * @return bool 
 */

function is_user_reg_matured( $reg_days_ago = 30 )
{
    $cu = wp_get_current_user();
    return ( isset( $cu->data->user_registered ) && strtotime( $cu->data->user_registered ) < strtotime( sprintf( \'-%d days\', $reg_days_ago ) ) ) ? TRUE : FALSE;    
}
这应该会返回TRUE 对于注册超过x 几天前,以及FALSE 针对访客(未登录)和其他用户。

用法示例:

if( is_user_reg_matured( 30 ) )
{
    // show content
}
快捷码:例如,您可以将其添加为快捷码:

[is_user_reg_matured reg_days_ago="30 ]

 Content for register matured users only

[/is_user_reg_matured]
使用:

add_shortcode( \'is_user_reg_matured\',   \'is_user_reg_matured_shortcode\' );

/**
 * Shortcode [is_user_reg_matured] to show content to register matured users only
 *
 * @link   http://wordpress.stackexchange.com/a/143597/26350
 * @param  array  $atts
 * @param  string $content
 * @return string $content
 */
function is_user_reg_matured_shortcode( $atts = array(), $content = \'\' )
{
    $atts = shortcode_atts( 
            array(
                \'reg_days_ago\' => \'30\',
            ), $atts, \'is_user_matured\' );

    if ( function_exists( \'is_user_reg_matured\' ) )
        if( is_user_reg_matured( (int) $atts[\'reg_days_ago\'] ) )
            return $content;

    return \'\';
}

SO网友:kaiser

首先,您需要以某种方式获得所需的用户(form?admin?foo?bar?baz?)

# Get Registration date
$reg_date = get_userdata( $user->ID )->user_registered;
# Get difference from now in seconds
$since = time() - strtotime( $reg_date );

# Show days since registration
# MORE info on date constants here
# @link http://wpkrauts.com/2013/wordpress-time-constants/
printf(
    __( \'%s days since the user registered\', \'your_textdomain\' ),
    $since * DAY_IN_SECONDS;
);

结束

相关推荐