How To Make an Auto-Generated Storage Code After User Registration

  • Function to generate 5-digit storage code
    Copy and paste this code:

    
    function jdl_generate_storage_code(){
    
      global $wpdb;
    
      $prefix = apply_filters( 'wpcsc_storage_code_prefix', 'BOX' );
    
      $randomize  = rand(11111,99999);
    
      $storage_code = $prefix.$randomize;
    
      $sql        = "SELECT COUNT(*) FROM $wpdb->usermeta WHERE `meta_key` LIKE '_jdl_storage_code' AND `meta_value` LIKE %s";
    
      $is_exist   = $wpdb->get_var( $wpdb->prepare( $sql, $storage_code ) );
    
      if( !$is_exist ){
    
        return $storage_code;
    
      }
    
      return jdl_generate_storage_code();
    
    }
    
  • Generate and save storage code when a user registers:
    Copy and paste this code:

    
    function jdl_generate_save_storage_code( $user_id, $user_data ){
    
      $unique_code = jdl_generate_storage_code();
    
      update_user_meta( $user_id, 'consolidation_code', $unique_code );
    
    add_action( 'wpcfe_after_user_registration_success', 'jdl_generate_save_storage_code', 10, 2 );
    
    
  • Generate and save storage code for existing users on update:
    Copy and paste this code:

    function jdl_generate_save_storage_code_for_existing_users( $user_data ){
    
      $_user_id = (int)$user_data->ID;
    
      if( !get_user_meta( $_user_id, 'consolidation_code', true ) ){
    
        update_user_meta( $_user_id, 'consolidation_code', jdl_generate_storage_code() );
    
      }
    
    }
    
    add_action( 'wpcumanage_after_save_user', 'jdl_generate_save_storage_code_for_existing_users', 10, 1 );
    
  • Show storage code on user’s list: ( requires WPCargo User Management Addon )
    Copy and paste this code:

    
    
    function wpcumanage_user_table_header_storage_code(){
    
      echo "".__('Storage Code', 'wpcargo-customizer')."";
    
    }
    
    add_action( 'wpcumanage_user_table_header', 'wpcumanage_user_table_header_storage_code' );
    
    function wpcumanage_user_table_data_storage_code( $user ){
    
      echo ''.get_user_meta( (int)$user->ID, 'consolidation_code', true ).'';
    
    }
    
    add_action( 'wpcumanage_user_table_data', 'wpcumanage_user_table_data_storage_code' );
    
    
0
    0
    Your Cart
    Your cart is emptyReturn to Shop