在注册用户时生成唯一编号

时间:2015-03-06 作者:pakwai122

我知道有userID, username 在数据库中,但我需要一个格式化的唯一数字,供物理世界使用。

例如:“2014xxx”201414:年,xxxx:注册用户时随机生成。

有可能吗?

如果可能,最简单和最快的方法是什么?

1 个回复
最合适的回答,由SO网友:David Gard 整理而成

这将实现您想要的功能,这两个功能都应该放在fucntions.php 文件

这个my_random_string() 函数接受参数,因此您可以在字符串之前/之后添加数据,以及更改字符串的长度和用于生成字符串的字符。

/**
 * Generate a string of random characters
 *
 * @param array $args   The arguments to use for this function
 * @return string|null  The random string generated by this function (only \'if($args[\'echo\'] === false)\')
 */
function my_random_string($args = array()){
    
    $defaults = array(  // Set some defaults for the function to use
        \'characters\'    => \'0123456789\',
        \'length\'        => 10,
        \'before\'        => \'\',
        \'after\'         => \'\',
        \'echo\'          => false
    );
    $args = wp_parse_args($args, $defaults);    // Parse the args passed by the user with the defualts to generate a final \'$args\' array
    
    if(absint($args[\'length\']) < 1) // Ensure that the length is valid
        return;
    
    $characters_count = strlen($args[\'characters\']);    // Check how many characters the random string is to be assembled from
    for($i = 0; $i <= $args[\'length\']; $i++) :          // Generate a random character for each of \'$args[\'length\']\'
    
        $start = mt_rand(0, $characters_count);
        $random_string.= substr($args[\'characters\'], $start, 1);
        
    endfor;
    
    $random_string = $args[\'before\'] . $random_string . $args[\'after\']; // Add the before and after strings to the random string
    
    if($args[\'echo\']) : // Check if the random string shoule be output or returned
        echo $random_string;
    else :
        return $random_string;
    endif;
    
}
给你my_on_user_register() 函数,每当生成新用户并将条目添加到wp_usermeta 对照表random_number 键,但显然可以根据需要更改此键的名称。

我还建议您看看Codex for the user_register action.

/**
 * Upon user registration, generate a random number and add this to the usermeta table
 *
 * @param required integer $user_id The ID of the newly registerd user
 */
add_action(\'user_register\', \'my_on_user_register\');
function my_on_user_register($user_id){

    $args = array(
        \'length\'    => 6,
        \'before\'    => date("Y")
    );
    $random_number = my_random_string($args);
    update_user_meta($user_id, \'random_number\', $random_number);

}
根据您的评论编辑回调函数my_on_user_register() 现在将生成一个数字,该数字从当前年份开始,然后以随机的6个字符串(仅数字)结束。

您也可以使用下面的my_extra_user_profile_fields() 回调函数,用于在用户配置文件页面上输出随机数。但是请注意,此代码不允许用户编辑该数字。

/**
 * Output additional data to the users profile page
 *
 * @param WP_User $user Object properties for the current user that is being displayed
 */
add_action(\'show_user_profile\', \'my_extra_user_profile_fields\');
add_action(\'edit_user_profile\', \'my_extra_user_profile_fields\');
function my_extra_user_profile_fields($user){
    
    $random_number = get_the_author_meta(\'random_number\', $user->ID);
?>
    <h3><?php _e(\'Custom Properties\'); ?></h3>
    
    <table class="form-table">
        <tr>
            <th><label for="address"><?php _e(\'Random Number\'); ?></label></th>
            <td><?php echo $random_number; ?></td>
        </tr>
    </table>
<?php
}

结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register