• I have a custom user metakey called phone_number, and i want the metakey to only accept unique value, which means two users can not have the same value in that meta key.
    Similar function with unique email address for each users.
    So the persons gets an error message that will say “phone number already used by another user, please try a different phone number”

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter pandglobal

    (@pandglobal)

    i have tried this yet is not working

    add_filter( 'gform_field_validation_19_1', 'custom_validationusd500', 10, 4 );
         function custom_validationusd500( $result, $value, $form, $field ) {
    	
    $args = array(
      'fields' => 'all',
     );
     
    $blogusers = get_users( $args);
    foreach($blogusers as $key => $user){
    	$usero = $user->user_login;
    	
    if ( $value != $usero ) {
            $result['is_valid'] = false;
            $result['message'] = 'The usd value must be $10 or above.';
        }
        return $result;
    }
    • This reply was modified 4 years, 10 months ago by Jan Dembowski.
    • This reply was modified 4 years, 10 months ago by bcworkz.
    Moderator bcworkz

    (@bcworkz)

    How one uses that specific filter is something I cannot comment on. However, to ensure unique user data in general, using a PHP loop is inefficient. I suggest using the WP_User_Query object to attempt to get a user by a specific meta key and value. If it succeeds, the value is already in use and cannot be used again.

    Thread Starter pandglobal

    (@pandglobal)

    Your suggestions seems perfect, but can i have a sample wp_query function that i can use to target the metakey phone_number
    And i will prefer to print out the results in an array format and in orderly list, and only the user meta key of phone_number should be printed out

    Thread Starter pandglobal

    (@pandglobal)

    My plan is to do this

    First step: use a code to print out all the users phone number located in the user metakey phone_number and i want to print them in an array.

    Step two. Then i will wrap my validation code using the in_array()

    Just that i don’t know the function that can print out all users phone number in an array format adding new line per each phone number.

    Moderator bcworkz

    (@bcworkz)

    See the docs page for a user meta query example in the user notes near the bottom of the page.
    https://developer.www.ads-software.com/reference/classes/wp_user_query/
    You don’t need to search for 3 different keys like the example, but the concept is the same.

    If your goal is to determine if a field value is already in place, what would be the point in printing out a list of values? Doing so publicly would more than likely be a privacy violation.

    If you use WP_User_Query to get a list of meta values for printing, it’s going to include a lot more data than you really want. To get a complete list of only one or two meta key values, I would query the usermeta table directly through $wpdb methods. You could print out the results in a structured manner with line feeds with print_r( $results );. But such output viewed as HTML would get all of its whitespace stripped out. It looks fine in source view though. To preserve whitespace in the normal HTML view, wrap the output with <pre> tags.

    If you prefer to print using a loop in order to follow a specific line format, output line feeds with echo "<br>\n";

    Thread Starter pandglobal

    (@pandglobal)

    thanks @bcworkz but i dont wish to use the $wpdb function as it makes me add my wordpress datatbase prefix in the function php, i am using the get users()

    and am able to get those user_email address printed out in a list or

     as you suggested, so printing it out for users to see is not the plan, because i dont intend to place it on the page body, i just wanted to retrieve those values and validate them. but then if i put out it shows list of users email adress in a list or preformat method, but if i add it on the validation code, only one email address from the list of email addresses on the site is printed out.
       ` $blogusers = get_users('meta_key=first_name');
            foreach ($blogusers as $user) {
            echo '<pre>' .$user->user_email . '</pre>';`
     
    the above code will list email addresses:
    
    [email protected]
    [email protected]
    [email protected]
    [email protected]
    
    but during validation only [email protected] is validated, others are not.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Restrict a user meta value to be unique across all users’ is closed to new replies.