Support

Account

Home Forums General Issues Add Role on registration

Helping

Add Role on registration

    • rhand

    • February 13, 2020 at 6:14 pm

    I wanted to use

    add_action( "user_register", function( $user_id ) {
      // $allowed = array( 'particulier', 'bedrijf' );
      $selected_radio = $_POST['acf']['field_5e44e2456a4a9'];
      $user = new WP_User( $user_id );
      if( $selected_radio == 'bedrijf') {
          $user->set_role($_POST['bedrijf']);
      } else {
          $user->set_role($_POST['particulier']);
    }
    } );

    to add users added as private person or business using radio button choice on registration form.

    This is the ACF used

    <?php
    if( function_exists('acf_add_local_field_group') ):
    
    acf_add_local_field_group(array(
    	'key' => 'group_5e44e23f16db9',
    	'title' => 'Custom Registration',
    	'fields' => array(
    		array(
    			'key' => 'field_5e44e2456a4a9',
    			'label' => 'Particulier of Bedrijf',
    			'name' => 'particulier_of_bedrijf',
    			'type' => 'radio',
    			'instructions' => 'Meld je aan als particulier of bedrijf',
    			'required' => 1,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'choices' => array(
    				'particulier' => 'particulier',
    				'bedrijf' => 'bedrijf',
    			),
    			'allow_null' => 0,
    			'other_choice' => 0,
    			'default_value' => '',
    			'layout' => 'vertical',
    			'return_format' => 'value',
    			'save_other_choice' => 0,
    		),
    	),
    	'location' => array(
    		array(
    			array(
    				'param' => 'user_form',
    				'operator' => '==',
    				'value' => 'register',
    			),
    		),
    	),
    	'menu_order' => 0,
    	'position' => 'normal',
    	'style' => 'seamless',
    	'label_placement' => 'top',
    	'instruction_placement' => 'label',
    	'hide_on_screen' => '',
    	'active' => true,
    	'description' => '',
    ));
    
    endif;

    Is this the right way to get the choice and add it? No role is added.

    Two, is https://www.advancedcustomfields.com/resources/acf-save_post/ still needed?

    • rhand

    • February 14, 2020 at 1:41 pm

    In the end to add the user role this did work

    add_action( "user_register", function( $user_id ) {
      $selected_radio = $_POST['field_5e44e2456a4a9'];
      $user = new WP_User( $user_id );
      if( $selected_radio == 'bedrijf') {
          $user->set_role('bedrijf');
    
      }
    } );
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.