0

I have created a phone number as Buddypress Profile field. I need to check that no two or more user accounts share the same phone number. My query below is not enforcing the restriction.

I need help to resolve this.

function bp_phone_number_validate() {
   global $bp;
   global $wpdb;

  // check if phone number is in use
  $result=$bp->get_var("SELECT COUNT(*) FROM {$bp->profile->wp_bp_xprofile_data} WHERE value = '{$_POST['field_2']}' GROUP BY id;");

  if($result > 0){
    $bp->signup->errors['field_2'] = __( 'Phone number is already in use.', 'buddypress' );
  }   
}
add_action( 'bp_signup_validate', 'bp_phone_number_validate');
0

There is no such thing as $bp->get_var. Try using $wpdb->get_var.

Or try this:

function validate_user_signup($result) {
   global $wpdb;

   $result = $wpdb->get_var("SELECT user_id FROM wp_bp_xprofile_data WHERE value = '{$_POST['field_2']}'");

   if( $result != NULL ){

      $new_error = new WP_Error();

      $new_error->add( 'field_2', __( 'Phone number is already in use.', 'buddypress' ) );

      $result['errors'] = $new_error;
   } 

   return $result;
}
add_filter('bp_core_validate_user_signup', 'validate_user_signup', 10, 1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.