Support

Account

Home Forums General Issues Add Role on registration

Solved

Add Role on registration

  • 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?

  • 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)

The topic ‘Add Role on registration’ is closed to new replies.