Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Gist_Member_Extension {
private function __construct() {
$this->add_hooks();
}
function add_hooks(){
add_filter( 'bp_signup_usermeta', [$this, 'pre_registration_filter'] );
}
/**
* Mutates $usermeta to ignore "add an institution" field and inject its
* value into "institution" field instead
* @param [array] $usermeta [the data from $_POST on registration page]
* @return [array] [possibly mutated $usermeta]
*/
public function pre_registration_filter($usermeta){
// debugging: output paramater
$this->debug($usermeta, '$usermeta');
// make sure these vars are clean for conditionals below
$new_institution_field = FALSE;
$institution_field = FALSE;
// find the institution field key if we need to add a new institution
$institution_field = array_search('Not listed', $usermeta);
// debugging: output institution field key
$this->debug($institution_field, '$institution_field');
// find the new institution field ($_POST does not provide field names)
$ids = array_reverse(explode(',',$usermeta['profile_field_ids']));
foreach($ids as $id){
$field = xprofile_get_field($id);
if($field -> name == "add an institution"){
$new_institution_field = 'field_'.$id;
// debugging: output the field key we found
$this->debug($usermeta[$new_institution_field], '$new_institution_field');
// we found it so end the loop
break;
}
}
if($institution_field){
if($new_institution_field){
// we are adding a new instiution
// replace institution='not listed' institution=with 'add an instution'
$usermeta[$institution_field] = $usermeta[$new_institution_field];
// destroy 'add an instutition' field
unset($usermeta[$new_institution_field]);
// debugging: output mutated field
$this->debug($usermeta[$institution_field], '$usermeta[$institution_field]');
}
}
return $usermeta;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment