0

How can I ensure that the buddypress extended custom profile field for phone number is unique, such that no two users can have the same phone numbers, just as is enforced for username?. I would appreciate if an existing documentation to handle this is pointed out to me, if it exists.

0

You should be able to enforce uniqueness with a filter something like this (untested...consider this pseudocode, just the basic idea):

add_filter( 'bp_xprofile_field_type_is_valid', function( $validated, $values, $field ) {
    global $bp, $wpdb;

    // you'll need to constrain this to your particular field type
    if ( $field->id === $your_field_id ) {
        $results = $wpdb->get_results( $wpdb->prepare(
            sprintf( "SELECT COUNT(*) FROM {$bp->profile->table_name_data} WHERE field_id = %s AND value = '%s'", $field->id, (string) $values )
        ) );
        $validated = ( count( $results ) === 0 );
    }

    return $validated;
}, 10, 3 );

I'm not aware of any documentation about this outside the code itself unfortunately. But take a look at the field type classes for xprofile - the code is pretty readable and there are decent comments as well.

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.