wordpress为用户添加自定义字段

前言

有时候,我们在进行wordpress开发时,总会遇到要给每个用户添加一个我们需要的字段,用以储存每个用户的配置。之前1500元为客户定制精简题库主题时(主题纯手写),我就遇到这样的问题,我需要在自己写的用户中心添加到期时间、用户余额、用户等级等字段。起初我先想的时在options表建立以用户ID加上固定字符为键值的设置字段,但后面我发现其实wordpress有自己的方式添加用户字段。两个方式都能实现,但是用wordpress的方式更加容易开发。我都简单说一下吧。

方法

1、options表储存用户字段的方法。

这种方法的话不太建议用,网上也少有这样的介绍,都是我自己想的,我在之前公司也确实用过这样的方式标记一些站点用户,使他们更加容易管理,也确实做到了这一点。

那时候我初学wordpress,就知道了options表(这个表就是我们设置的数据库前缀+options,默认是wp_options)的一些操作,其中包含get_option、add_option、update_option、delete_option四个函数,分别是:获取、新增、更新(无则添加)、删除options表的内容。

图片[1]-wordpress为用户添加自定义字段
options表结构

看到上面的表结构,学习了上面的函数,你也会想到,为每个用户新增一行记录,用来当作用户字段使用。但你仔细观察这个表,就能发现,这个表有大量开发者使用,主要用来存储一些设置参数等,官方设计这个表也是为了存储一些设置参数,但是有些时候你会看到这个表比我们的文章数量还多,所以我们不推荐使用这个方法来新增我们的用户自定义字段,但也不是不可以用。比如我们要新增一个用户余额,我们可以这样预想:用用户ID和固定字符串为键值来储存信息。比如我要更改一个用户的余额为10,我们可以使用update_option函数,这样使用:

update_option("yue".$userid,10);//注意,yue为余额的拼音(可以自己弄一个固定的),$userid为用户的ID

当我们需要获取某个用户的余额时候,我们可以这样:

$yue = get_option("yue".$userid);

2、wordpress官方推荐的用户字段处理

这里我就放一段代码吧,你们自己看一下,应该也是不难,代码也是从wordpress官网抄来的,然后自己改了一下,是一个剩余次数的字段,主要三个功能,用户自己编辑界面、管理员编辑界面和添加用户管理界面的表头三个功能,代码如下:

function wporg_usermeta_form_field_User_Times_Balance( $user )
{
    ?>
    <br>
    <h3>剩余次数</h3>
    <table class="form-table">
        <tr>
            <th>
                <label for="User_Times_Balance">剩余次数</label>
            </th>
            <td>
                <input type="date"
                       class="regular-text ltr"
                       id="User_Times_Balance"
                       name="User_Times_Balance"
                       value="<?= esc_attr( get_user_meta( $user->ID, 'User_Times_Balance', true ) ) ?>"
                       title="Please use YYYY-MM-DD as the date format."
                       pattern="(19[0-9][0-9]|20[0-9][0-9])-(1[0-2]|0[1-9])-(3[01]|[21][0-9]|0[1-9])"
                       required>
                <p class="description">
                    用户剩余次数<span style="color:red">小于1</span>,用户就不能使用!
                </p>
            </td>
        </tr>
    </table>
    <?php
}
  
/**
 * The save action.
 *
 * @param $user_id int the ID of the current user.
 *
 * @return bool Meta ID if the key didn't exist, true on successful update, false on failure.
 */
function wporg_usermeta_form_field_User_Times_Balance_update( $user_id )
{
    // check that the current user have the capability to edit the $user_id
    if ( ! current_user_can( 'edit_user', $user_id ) ) {
        return false;
    }
  
    // create/update user meta for the $user_id
    return update_user_meta(
        $user_id,
        'User_Times_Balance',
        $_POST['User_Times_Balance']
    );
}


// Add the field to user profile editing screen.将该字段添加到用户资料编辑界面。//管理员编辑其他用户
add_action(
    'edit_user_profile',
    'wporg_usermeta_form_field_User_Times_Balance'
);
  

  
// Add the save action to user profile editing screen update.在用户资料编辑界面更新中添加保存动作。//管理员编辑其他用户
add_action(
    'edit_user_profile_update',
    'wporg_usermeta_form_field_User_Times_Balance_update'
);


//Adds Custom Column To Users List Table 将字段添加到用户列表的表头
function custom_add_User_Times_Balance($columns) {
    $columns['User_Times_Balance'] = '剩余次数';
    return $columns;
}
add_filter('manage_users_columns', 'custom_add_User_Times_Balance');//表头
//Adds Content To The Custom Added Column
function custom_show_User_Times_Balance($value, $column_name, $user_id) {
    $enddate = get_user_meta( $user_id, 'User_Times_Balance',true);
    if ( 'User_Times_Balance' == $column_name ){
        if($enddate){
            return $enddate;
        }else{
            return "--";
        }
    }
}
add_filter('manage_users_custom_column',  'custom_show_User_Times_Balance', 10, 3);//内容

结语

文章属于KEKC博客原创,转载请说明转载于https://www.kekc.cn/

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容