Home › Forums › ACF PRO › ACF PRO Registration member › Reply To: ACF PRO Registration member
Let’s say that you create a select type field with the values
group_1 : Group 1
group_2 : Group 2
group_3 : Group 3
group_4 : Group 4
the values are on the left, and that you also create user roles that match the values.
add_action('acf/save_post', 'add_user_role_from_acf', 20);
function add_user_role_from_acf($post_id) {
if (substr($post_id, 0, 5) != 'user_') {
// not saving a user
return;
}
if (empty(get_field('your_field_name', $post_id)) {
// no value saved in user group field
return;
}
$role = get_field('your_field_name', $post_id);
$user_id = intval(substr($post_id, 5));
$user = new WP_User($user_id);
$user->set_role($role);
}
One not is that you would only want to show the field on the new user registration form and not on edit user page because this function would not distinguish between adding a user and updating a user. The field would not be needed in the admin because there if it needed to be changed you would do that using the WP Role setting field.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.