How To Add Custom Filter for User Unique IDs

1. Copy this code and paste it on your active theme’s functions.php file:

function ali_custom_unique_code_filter_callback(){
  global $current_user;
  $role = $current_user->roles[0];
  if ( $role !== 'administrator') { return; }
  $_unicode = ( isset( $_GET['_unicode'] ) && !empty( $_GET['_unicode'] ) ) ? $_GET['_unicode'] : 0;</pre>
<div class="form-group wpcfe-filter <?php echo $role; ?> unicode-filter p-0 mx-1"><select id="unicode-filter" class="form-control md-form wpcfe-select" name="_unicode">
<option value=""><!--?php _e('All User Unique IDs', 'wpcargo-customizer'); ?--></option>    <!--?php foreach( all_client_ids() as $client_id => $unique_code ): $selected = $_unicode == $client_id ? 'selected' : ''; ?-->
<option selected="selected" value="<?php echo $client_id; ?>">><!--?php echo $unique_code; ?--></option>        <!--?php endforeach; ?-->
</select></div>
<pre> <!--?php 
}
add_action('wpcfe_after_shipment_filters', 'ali_custom_unique_code_filter_callback');

 

2. To make the filter work, copy this code:

function ali_custom_meta_query_callback( $meta_query ){
  if( isset($_GET['_unicode']) && !empty( $_GET['_unicode'] ) ){
    $_unicode = $_GET['_unicode'];
    $meta_query[] = array(
      'key'     => 'registered_shipper',
      'value'   => $_unicode,
      'compare' => '='
    );
  }
  return $meta_query;
}

add_filter( 'wpcfe_dashboard_meta_query', 'ali_custom_meta_query_callback', 10, 1 );

 

As you have noticed on number 1, we used a function to loop the IDs, here is the function that returns a formatted array with client IDs as keys and their generated unique code as values:

function all_client_ids() {
  $result = array();
  $args   = array(
    'role'   => 'wpcargo_client',
    'fields' => 'ids'
  );
  $user_ids  = get_users( $args );
  foreach( $user_ids as $user_id ) {
    $_user_unique_code = get_user_meta( $user_id, '_ali_unique_user_code', true );
    if ( !empty( $_user_unique_code ) ) {
      $result[$user_id]  = $_user_unique_code;
    }
  }
  return $result;
}