Hi
I’ve created an ACF Form that creates a custom post type. Within the form there is a radio button that has three values – General | Food | Hot Food
What I’d like to be able to do is append the already established roles for these to the user based on the selection of the radio button.
Is this possible?
You add an acf/save_post action. In the action you get the value of the field and you get the user you want to add the role to and then you use $user->add_role().
I don’t have any code example. It would really depend on what user you’re adding the role to. The user being edited or the user being updated. Also you’d have to deal with the eventuality of the value being changed so you’d need to look at removing roles as well.
Thanks John worked it out from your input.
Here’s the code if anyone else needs help as I looked around for quite a while before raising a ticket.
add_action (‘acf/save_post’, ‘add_role_on_selection’);
function add_role_on_selection ($post_id) {
// Get newly saved values.
$values = get_fields( $post_id );
// Check the new value of a specific field.
$selected = get_field(‘stall_catergory’, $post_id);
$user = wp_get_current_user();
if( $selected == ‘general’) {
$user->add_role(‘general’);
}
if( $selected == ‘food’) {
$user->add_role(‘food’);
}
if( $selected == ‘hot food’) {
$user->add_role(‘hot_food’);
}
}