Created
April 22, 2019 13:02
-
-
Save doubleedesign/322717cba0a75749e2bce35c0762608c to your computer and use it in GitHub Desktop.
Ninja Forms + Groups. When a user registers for an account using Ninja Forms User Management (https://ninjaforms.com/extensions/user-management), assign them to a group (https://wordpress.org/plugins/groups) according to the value of a drop-down selection which matches the "slug" format equivalent of the group name.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get all groups | |
* Utility function | |
* @author Antonio Blanco | |
* @see https://github.com/eggemplo/Groups-Utilities/blob/master/get_groups.php | |
*/ | |
function doublee_get_all_groups() { | |
global $wpdb; | |
$groups_table = _groups_get_tablename('group'); | |
return $wpdb->get_results( "SELECT * FROM $groups_table ORDER BY name" ); | |
} | |
/** | |
* Get group name by ID | |
* Utility function | |
* @param $group_id | |
* | |
* @return mixed | |
*/ | |
function doublee_get_group_name($group_id) { | |
global $wpdb; | |
$groups_table = _groups_get_tablename('group'); | |
$results = $wpdb->get_results( "SELECT `name` FROM $groups_table WHERE `group_id` = $group_id"); | |
$name = $results[0]->name; | |
return $name; | |
} | |
/** | |
* Get group ID by slug | |
* Utility function | |
* Note: Groups don't actually have slugs, so this assumes that running doublee_build_title_slug on the name will yield the expected slug. | |
* @param $group_slug | |
* | |
* @return mixed | |
*/ | |
function doublee_get_group_id($group_slug) { | |
$id = ''; | |
// Get all the groups | |
$groups = doublee_get_all_groups(); | |
// Loop through and find the one matching the provided slug | |
foreach($groups as $group) { | |
$slug = doublee_build_title_slug($group->name); | |
if($slug == $group_slug) { | |
$id = $group->group_id; | |
} | |
} | |
// Return the group ID | |
return $id; | |
} | |
/** | |
* Create processing hook to add to Ninja Forms submission action | |
* | |
* @tag doublee_add_new_user_to_group | |
* @callback doublee_add_new_user_to_group | |
*/ | |
add_action('doublee_user_form_process_hook', 'doublee_add_new_user_to_group'); | |
/** | |
* When user account is created using the Ninja Form, | |
* assign the user to the deb group they selected | |
* | |
* @param $form_data array | |
* @return void | |
*/ | |
function doublee_add_new_user_to_group($form_data) { | |
$form_fields = $form_data['fields']; | |
foreach($form_fields as $field) { | |
// Find the deb group field | |
if( 'debutante_group' == $field['key']) { | |
// The selected group, should be in slug format | |
$selected_group = $field['value']; | |
// Get the group ID | |
$group_id = doublee_get_group_id($selected_group); | |
} | |
// Find the email address (username) field | |
if( 'email_address' == $field['key']) { | |
$email = $field['value']; | |
// Get the user's ID | |
$user = get_user_by('login', $email); | |
$user_id = $user->ID; | |
} | |
// Assign user to group | |
if(isset($group_id) && isset($user_id)) { | |
Groups_User_Group::create( array( | |
'user_id' => $user_id, | |
'group_id' => $group_id | |
) ); | |
} else { | |
error_log("Couldn't add user to group. Likely a problem with finding the desired user ID or group ID."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment