Support

Account

Home Forums General Issues User role / User group field type

Solved

User role / User group field type

  • Hi, I wonder to know if is there any quick trick to be able to list all the user groups/user roles active in my WP and allow an user to select them using a dropdown.

    I noticed that using user type I can see them but are disabled.

  • Hi @mmn

    To achieve this select field, you can populate the choices dynamically following this tutorial:
    http://www.advancedcustomfields.com/resources/tutorials/dynamically-populate-a-select-fields-choices/

    Firstly, create a select field,
    Next, add the filter code to populate the choices of the $field
    Jump on google to find the WP code to get a list of all user roles
    Loop through the user roles and append them to the $field[‘choices’]

    Easy

    Thanks
    E

  • Thanks @elliot!

    Here the snippet I used, hope it can help other ppl if are doing the same thing:

    add_filter('acf/load_field/name=user_groups', 'populateUserGroups');
    
    function populateUserGroups( $field )
    {	
    	// reset choices
    	$field['choices'] = array();
    	
    	global $wp_roles;
    	$roles = $wp_roles->get_names();
    	
    	foreach ($roles as $role) {
    		$field['choices'][ $role ] = $role;
    	}
    
    	return $field;
    }
  • Hey,

    is there a way to limit the user roles to only custom/subscribers user roles?

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘User role / User group field type’ is closed to new replies.