pmpro_getMembershipLevelsForUser

Pmpro_getMembershipLevelsForUser() returns the membership levels for a user.

Description

pmpro_getMembershipLevelsForUser( (constant) $user_id = NULL, (constant) $include_inactive = false ); 

If $user_id is omitted, the value will be retrieved from $current_user. By default it only includes actvie memberships.

Return values: Success returns an array of level objects. Failure returns false.

Parameters (2)

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

Usage

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

Defined (1)

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

/includes/functions.php  
  1. function pmpro_getMembershipLevelsForUser($user_id = NULL, $include_inactive = 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 $wpdb; 
  13.  
  14. $levels = $wpdb->get_results("SELECT 
  15. l.id AS ID,  
  16. l.id as id,  
  17. mu.id as subscription_id,  
  18. l.name,  
  19. l.description,  
  20. l.expiration_number,  
  21. l.expiration_period,  
  22. mu.initial_payment,  
  23. mu.billing_amount,  
  24. mu.cycle_number,  
  25. mu.cycle_period,  
  26. mu.billing_limit,  
  27. mu.trial_amount,  
  28. mu.trial_limit,  
  29. mu.code_id as code_id,  
  30. UNIX_TIMESTAMP(startdate) as startdate,  
  31. UNIX_TIMESTAMP(enddate) as enddate 
  32. FROM {$wpdb->pmpro_membership_levels} AS l 
  33. JOIN {$wpdb->pmpro_memberships_users} AS mu ON (l.id = mu.membership_id) 
  34. WHERE mu.user_id = $user_id".($include_inactive?"":" AND mu.status = 'active'")); 
  35. /** 
  36. * pmpro_get_membership_levels_for_user filter. 
  37. * Filters the returned levels. 
  38. * @since 1.8.5.4 
  39. * @param array $levels Array of level objects. 
  40. */ 
  41. $levels = apply_filters('pmpro_get_membership_levels_for_user', $levels, $user_id); 
  42.  
  43. return $levels;