Skip to content

Instantly share code, notes, and snippets.

@csaborio001
Created November 18, 2019 23:21
Show Gist options
  • Save csaborio001/b619a85df2279590b1f2b1745bb956b7 to your computer and use it in GitHub Desktop.
Save csaborio001/b619a85df2279590b1f2b1745bb956b7 to your computer and use it in GitHub Desktop.
Gravity Forms allows you to create a new WordPress user. Profile Builder Pro allows you to edit those fields in User Meta as long as they have the same name. If the field is multi-select, GF saves the values as ["Value1", "Value2"], Profile Builder does not like this. This snippet will intercept the user creation *AFTER* the meta has been writte…
/**
* When a new user is created it goes through the user meta to find entries that
* have the format ["Value","Value1"] and replaces it with Value,Value1. GravityForms
* writes multi-select form fields this way and the ProfileBuilder Pro does not understand it,
* so we need to replace the entry for PBP to gather what these values are.
*
* @param int @user_id - the ID if the user that was just created
*
* @return void
*/
public function gf_pbp_multiselect_fix( $user_id ) {
$user_meta_fields = get_user_meta( $user_id );
$gf_regex_identifier = '/\["\w.+"\]/';
foreach ( $user_meta_fields as $user_meta_field_key => $user_meta_field_value ) {
if ( is_array( $user_meta_field_value ) &&
1 === count( $user_meta_field_value ) &&
preg_match( $gf_regex_identifier, $user_meta_field_value[0] ) ) {
$gf_regex_target_replace = '/[\[""\]]/';
$new_value = preg_replace( $gf_regex_target_replace, '', $user_meta_field_value[0] );
$result = update_user_meta( $user_id, $user_meta_field_key, $new_value );
if ( WP_DEBUG && $result ) {
error_log( 'Successfully changed GF meta to ' . $new_value . ' for user ' . $user_id );
} elseif ( WP_DEBUG && ! $result ) {
error_log( 'Failed changing GF meta key ' . $user_meta_field_key . ' for user ' . $user_id );
}
}
}
}
add_action( 'gform_user_registered', 'gf_pbp_multiselect_fix', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment