Creating new table row from metavalue in User Registration

I am integrating Gravity Forms, Gravity Forms User Registration Add On and Groups by ithinx.

I have written the following function:

add_action( 'edit_user_created_user', 'hl_add_group', 10, 1 ); 

function hl_add_group($user_id,$notify) {

	$group = get_user_meta($user_id,'hl_user_group');
	
  	global $wpdb;
  
  	// add form data to custom database table
	$wpdb->insert(
	    'wp_groups_user_group',
	    array(
	      'user_id' => $user_id,
	      'group_id' => $group,
	    )
	);
}

The custom meta field h1_user_group is created based on a Gravity Forms field. The value stored corresponds to a Group ID.

I have tried hooking user_registration and edit_user_created_user. When using user_registration, it successfully creates the new role, but get_user_meta returns 0 because the meta hasn’t been added yet. When using the version above, nothing is added.

I appreciate your help very much!

Have you tried using the gform_user_registered hook?

That worked! Thank you. I searched through support and only found hooks for form submission, which didn’t work. Thank you! Here’s the modified code for anyone else who may want it…

add_action( 'gform_user_registered', 'hl_add_group', 10, 4 );
function hl_add_group($user_id, $feed, $entry) {

$group = $entry[8];

global $wpdb;

// add form data to custom database table
$wpdb->insert(
    'wp_groups_user_group',
        array(
          'user_id' => $user_id,
          'group_id' => $group,
        ),
        array(
	        '%d',
	        '%d'
        )
    );
}

Thank you for sharing that code. Glad you found something that worked.

1 Like