Advertisement
designbymerovingi

myCRED Transfer by Display Name

Jul 18th, 2017
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.48 KB | None | 0 0
  1. //Add custom transfer fields for giving credits to creators
  2. function mycred_pro_custom_transfer_compl( $options ) {
  3.  
  4.     $options['display_name'] = 'Display Name';
  5.     return $options;
  6.  
  7. }
  8. add_filter( 'mycred_transfer_autofill_by', 'mycred_pro_custom_transfer_compl' );
  9.  
  10. // Take over the autocomplete process
  11. function mycred_pro_custom_autocomplete( $transfer_settings, $mycred ) {
  12.  
  13.     $results = array();
  14.     $user_id = get_current_user_id();
  15.     $string  = sanitize_text_field( $_REQUEST['string']['term'] );
  16.  
  17.     global $wpdb;
  18.  
  19.     // Query
  20.     $blog_users = $wpdb->get_results( $wpdb->prepare( "SELECT display_name, ID FROM {$wpdb->users} WHERE ID != %d AND display_name LIKE %s;", $user_id, '%' . $string . '%' ), 'ARRAY_N' );
  21.  
  22.     if ( $wpdb->num_rows > 0 ) {
  23.  
  24.         foreach ( $blog_users as $hit ) {
  25.  
  26.             if ( $mycred->exclude_user( $hit[1] ) ) continue;
  27.             $results[] = $hit[0];
  28.  
  29.         }
  30.  
  31.     }
  32.  
  33.     wp_send_json( $results );
  34.  
  35. }
  36. add_action( 'mycred_transfer_autofill_find', 'mycred_pro_custom_autocomplete', 10, 2 );
  37.  
  38. // Get the recipient
  39. function mycred_pro_find_transfer_recipient( $recipient_id, $to ) {
  40.  
  41.     if ( $recipient_id !== false ) return $recipient_id;
  42.  
  43.     global $wpdb;
  44.  
  45.     // Get user by display name
  46.     $recipient = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->users} WHERE display_name = %s", $to ) );
  47.     if ( $recipient !== NULL )
  48.         $recipient_id = $recipient;
  49.  
  50.     return $recipient_id;
  51.  
  52. }
  53. add_filter( 'mycred_transfer_get_recipient', 'mycred_pro_find_transfer_recipient', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement