pmpro_getMembershipLevelForUser

Pmpro_getMembershipLevelForUser() returns the first active membership level for a user.

Description

pmpro_getMembershipLevelForUser( (constant) $user_id = NULL, (constant) $force = false ); 

If $user_id is omitted, the value will be retrieved from $current_user.

Return values: Success returns the level object. Failure returns false.

Parameters (2)

0. $user_id — Optional. (constant) => NULL
The user id.
1. $force — Optional. (constant) => false
The force.

Usage

  1. if ( !function_exists( 'pmpro_getMembershipLevelForUser' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/includes/functions.php'; 
  3.  
  4. // The user id. 
  5. $user_id = NULL; 
  6.  
  7. // The force. 
  8. $force = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = pmpro_getMembershipLevelForUser($user_id, $force); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/functions.php  
  1. function pmpro_getMembershipLevelForUser($user_id = NULL, $force = false) 
  2. if(empty($user_id)) 
  3. global $current_user; 
  4. $user_id = $current_user->ID; 
  5.  
  6. if(empty($user_id)) 
  7. return false; 
  8.  
  9. //make sure user id is int for security 
  10. $user_id = intval($user_id); 
  11.  
  12. global $all_membership_levels; 
  13.  
  14. if(isset($all_membership_levels[$user_id]) && !$force) 
  15. return $all_membership_levels[$user_id]; 
  16. else 
  17. global $wpdb; 
  18. $all_membership_levels[$user_id] = $wpdb->get_row("SELECT 
  19. l.id AS ID,  
  20. l.id as id,  
  21. mu.id as subscription_id,  
  22. l.name AS name,  
  23. l.description,  
  24. l.expiration_number,  
  25. l.expiration_period,  
  26. l.allow_signups,  
  27. mu.initial_payment,  
  28. mu.billing_amount,  
  29. mu.cycle_number,  
  30. mu.cycle_period,  
  31. mu.billing_limit,  
  32. mu.trial_amount,  
  33. mu.trial_limit,  
  34. mu.code_id as code_id,  
  35. UNIX_TIMESTAMP(startdate) as startdate,  
  36. UNIX_TIMESTAMP(enddate) as enddate 
  37. FROM {$wpdb->pmpro_membership_levels} AS l 
  38. JOIN {$wpdb->pmpro_memberships_users} AS mu ON (l.id = mu.membership_id) 
  39. WHERE mu.user_id = $user_id AND mu.status = 'active' 
  40. LIMIT 1"); 
  41.  
  42. /** 
  43. * pmpro_get_membership_level_for_user filter. 
  44. * Filters the returned level. 
  45. * @since 1.8.5.4 
  46. * @param object $level Level object. 
  47. */ 
  48. $all_membership_levels[$user_id] = apply_filters('pmpro_get_membership_level_for_user', $all_membership_levels[$user_id], $user_id); 
  49.  
  50. return $all_membership_levels[$user_id];