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

actions - Assign user role by text field in Wordpress (Woocommerce)

programmeradmin1浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

I have a virtual store with woocommerce where I use the USER REGISTRATION plugin to register new customers. This plugin allows the creation of custom fields. I created a custom text field called "STATE REGISTRATION". In this field the user can write something or leave it blank.

My demand is that when creating a new user, wordpress will read this "STATE REGISTRATION" field and see if there is any content filled in it.

If you have something filled out, I need the user to have a second "ROLE" added to their profile.

I created this role before and it's called "IE".

I'm try this, but doesnt works!

add_action( 'user_registration_after_register_user_action', 'adiciona_role_ie', 9999, 3 );
function adiciona_role_ie( $valid_form_data, $form_id, $user_id ) {
    $billing_ie = '';
    if ( isset( $valid_form_data['billing_ie'] ) && ! empty( $valid_form_data['billing_ie']->value ) ) {
        echo $billing_ie = $valid_form_data['billing_ie']->value;
    }
    if ( ! empty( $billing_ie ) ) {
    
    add_filter('user_registration_after_register_user_action', 'wc_assign_custom_role', 10, 1);

    function wc_assign_custom_role($args) {
      $args['role'] = 'ie';
      
      return $args;
    }
        //update_user_meta( $user_id, 'ie', $role );
    }
}

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

I have a virtual store with woocommerce where I use the USER REGISTRATION plugin to register new customers. This plugin allows the creation of custom fields. I created a custom text field called "STATE REGISTRATION". In this field the user can write something or leave it blank.

My demand is that when creating a new user, wordpress will read this "STATE REGISTRATION" field and see if there is any content filled in it.

If you have something filled out, I need the user to have a second "ROLE" added to their profile.

I created this role before and it's called "IE".

I'm try this, but doesnt works!

add_action( 'user_registration_after_register_user_action', 'adiciona_role_ie', 9999, 3 );
function adiciona_role_ie( $valid_form_data, $form_id, $user_id ) {
    $billing_ie = '';
    if ( isset( $valid_form_data['billing_ie'] ) && ! empty( $valid_form_data['billing_ie']->value ) ) {
        echo $billing_ie = $valid_form_data['billing_ie']->value;
    }
    if ( ! empty( $billing_ie ) ) {
    
    add_filter('user_registration_after_register_user_action', 'wc_assign_custom_role', 10, 1);

    function wc_assign_custom_role($args) {
      $args['role'] = 'ie';
      
      return $args;
    }
        //update_user_meta( $user_id, 'ie', $role );
    }
}

Share Improve this question edited Jan 6, 2021 at 16:13 Lucas Catani asked Jan 5, 2021 at 21:01 Lucas CataniLucas Catani 12 bronze badges 2
  • 1 Hi, Lucas, welcome to WPSE. In order to get a faster response, I suggest you add a little bit more of context, and information to your question and explain what you already tried, a wee a little bit about your knowledge and skill with WordPress. – Celso Bessa Commented Jan 5, 2021 at 21:54
  • Hi, try to read again. Thanks. – Lucas Catani Commented Jan 6, 2021 at 16:13
Add a comment  | 

1 Answer 1

Reset to default 0

Here is the code:

function ur_update_role( $valid_form_data, $form_id, $user_id ) {
global $table_prefix;
$assign_roles_list = array();
if( isset( $valid_form_data['billing_ie']) && !empty( $valid_form_data['billing_ie']->value ) ) {
array_push( $assign_roles_list, 'ie' );
}
if ( ! empty( $assign_roles_list ) ) {
// Re-ordering roles according to priority.
$user_roles_list = ur_get_default_admin_roles();
foreach ( $user_roles_list as $key => $value ) {
if ( ! in_array( $key, $assign_roles_list, true ) ) {
unset( $user_roles_list[ $key ] );
} else {
$user_roles_list[ $key ] = true;
}
}
$field_name = $table_prefix . 'capabilities';
update_user_meta( $user_id, $field_name, $user_roles_list );
}
}
发布评论

评论列表(0)

  1. 暂无评论