0

I have added the ability for a social security number to be added for users. This field can be edited on the individual users screen but the data is saved to the usermeta table. When editing a new user, I want to check if that number is being used somewhere else for another user as duplicates should not be allowed.

My initial code is:

function save_cust_user_profile_fields( $user_id ) {

     if ( isset($_POST['social_number']) ) {
       update_user_meta( $user_id, 'social_number', intval($_POST['social_number']));
     }

}

add_action( 'personal_options_update', 'save_cust_user_profile_fields' );

add_action( 'edit_user_profile_update', 'save_cust_user_profile_fields' );

But this obviously allows duplicates which it shouldn't.

1

You can check duplicate user meta using sql query.in below code if duplicate record found of other users then it will display error message and not update value. add below code in active theme's functions.php file.

function save_cust_user_profile_fields( $user_id ) {

     if ( isset($_POST['social_number']) ) {
        global $wpdb;
        $social_number = $_POST['social_number'];
         # query to check duplicate value
        $query = "SELECT $wpdb->users.ID  FROM $wpdb->users  INNER JOIN $wpdb->usermeta ON ( $wpdb->users.ID = $wpdb->usermeta.user_id )  INNER JOIN $wpdb->usermeta AS mt1 ON ( $wpdb->users.ID = mt1.user_id ) WHERE 1=1  AND 
          ( $wpdb->usermeta.meta_key = 'social_number' AND $wpdb->usermeta.meta_value = $social_number ) ";

        $result = $wpdb->get_results($query);
        #if result found then redirect with error msg
        if($result && $user_id != $result[0]->ID){
            add_filter( 'wp_redirect', 'add_notice_query_variable', 99 );
        }
        else
        {  
            # if not found duplicate then update
            update_user_meta( $user_id, 'social_number', intval($_POST['social_number']));            
        }

     }

}
add_action( 'personal_options_update', 'save_cust_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_cust_user_profile_fields' );

#Add query var in url for display error message 
function add_notice_query_variable( $location ) {
   remove_filter( 'wp_redirect', 'add_notice_query_variable' , 99 );
   return add_query_arg( array( 'social_number' => $_POST['social_number'] ), $location );
}

# display duplicate error admin notice
add_action( 'admin_notices',  'admin_notices_callback');
function admin_notices_callback() {
   if ( ! isset( $_GET['social_number'] )) {
     return;
   }
   ?>
   <div class="error notice">
      <p><?php esc_html_e( 'social_number : '.$_GET['social_number'].' already used in other user, Please try different!', '' ); ?></p>
   </div>
   <?php
  }

I have tested and it is working fine for me. https://prnt.sc/rau4ug let me know if this works for you.

| improve this answer | |

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.