最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Saving user meta "member_id" based on user role

programmeradmin3浏览0评论

In a WP / Woocommerce with subscriptions plugin environment I manage to save in the user meta table a custom "member_id" at the user creation level:

function assignuserid($user_id) {
    global $wpdb;
    $latestid=$wpdb->get_var("SELECT meta_value from $wpdb->usermeta where meta_key='member_id' order by meta_value DESC limit 1;");
    update_user_meta( $user_id, 'member_id', $latestid+1 );
}
add_action( 'user_register', 'assignuserid');

Important: this is working for users of all roles (such as administrator or subscriber) both from the admin backoffice (Users > Add New) and the woocommerce order user registration form.

Now, when I try to have the member_id generated only for user role = "subscriber" using the code

function assignuserid($user_id) {
    if ( user_can( $user_id, 'subscriber')) {
        global $wpdb;
        $latestid=$wpdb->get_var("SELECT meta_value from $wpdb->usermeta where meta_key='member_id' order by meta_value DESC limit 1;");
        update_user_meta( $user_id, 'member_id', $latestid+1 );
    }
}
add_action( 'user_register', 'assignuserid');

This is working well for users created from the admin backoffice: administrators do not get the member_id, subscribers get the member_id BUT, none of the users created via the woocommerce order user registration form get the user-meta "member_id" field. They are subscribers. I don't understand why...

In a WP / Woocommerce with subscriptions plugin environment I manage to save in the user meta table a custom "member_id" at the user creation level:

function assignuserid($user_id) {
    global $wpdb;
    $latestid=$wpdb->get_var("SELECT meta_value from $wpdb->usermeta where meta_key='member_id' order by meta_value DESC limit 1;");
    update_user_meta( $user_id, 'member_id', $latestid+1 );
}
add_action( 'user_register', 'assignuserid');

Important: this is working for users of all roles (such as administrator or subscriber) both from the admin backoffice (Users > Add New) and the woocommerce order user registration form.

Now, when I try to have the member_id generated only for user role = "subscriber" using the code

function assignuserid($user_id) {
    if ( user_can( $user_id, 'subscriber')) {
        global $wpdb;
        $latestid=$wpdb->get_var("SELECT meta_value from $wpdb->usermeta where meta_key='member_id' order by meta_value DESC limit 1;");
        update_user_meta( $user_id, 'member_id', $latestid+1 );
    }
}
add_action( 'user_register', 'assignuserid');

This is working well for users created from the admin backoffice: administrators do not get the member_id, subscribers get the member_id BUT, none of the users created via the woocommerce order user registration form get the user-meta "member_id" field. They are subscribers. I don't understand why...

Share Improve this question edited May 4, 2020 at 13:47 WPNow asked May 4, 2020 at 11:09 WPNowWPNow 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0
Try this:

function assignuserid($user_id) {

    $user_meta = get_userdata($user_id); 

    $user_roles = $user_meta->roles;

    if (in_array("subscriber", $user_roles)){
        global $wpdb;
        $latestid=$wpdb->get_var("SELECT meta_value from $wpdb->usermeta where meta_key='member_id' order by meta_value DESC limit 1;");
        update_user_meta( $user_id, 'member_id', $latestid+1 );
    }
}
add_action( 'user_register', 'assignuserid');
发布评论

评论列表(0)

  1. 暂无评论