pmpro_shortcode_membership

Shortcode to hide/show content based on membership level.

Description

pmpro_shortcode_membership( $atts, (constant) $content = null, (string) $code = '' ); 

Parameters (3)

0. $atts
The atts.
1. $content — Optional. (constant) => null
The content.
2. $code — Optional. (string) => ''
The code.

Usage

  1. if ( !function_exists( 'pmpro_shortcode_membership' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/shortcodes/membership.php'; 
  3.  
  4. // The atts. 
  5. $atts = null; 
  6.  
  7. // The content. 
  8. $content = null; 
  9.  
  10. // The code. 
  11. $code = ''; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = pmpro_shortcode_membership($atts, $content, $code); 
  15.  

Defined (1)

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

/shortcodes/membership.php  
  1. function pmpro_shortcode_membership($atts, $content=null, $code="") 
  2. // $atts ::= array of attributes 
  3. // $content ::= text within enclosing form of shortcode element 
  4. // $code ::= the shortcode found, when == callback name 
  5. // examples: [membership level="3"]...[/membership] 
  6.  
  7. extract(shortcode_atts(array( 
  8. 'level' => NULL,  
  9. 'levels' => NULL,  
  10. 'delay' => NULL 
  11. ), $atts)); 
  12.  
  13. //if levels is used instead of level 
  14. if(isset($levels) && !isset($level)) 
  15. $level = $levels; 
  16.  
  17. global $wpdb, $current_user; 
  18.  
  19. //guilty until proven innocent :) 
  20. $hasaccess = false; 
  21.  
  22. //figure out which level/levels to check 
  23. if(!empty($level) || $level === "0" || $level === 0) 
  24. //they specified a level(s) 
  25. if(strpos($level, ", ")) 
  26. //they specified many levels 
  27. $levels = explode(", ", $level); 
  28. else 
  29. //they specified just one level 
  30. $levels = array($level); 
  31. }  
  32. else 
  33. //didn't specify a membership level, so use false so pmpro_hasMembershipLevel checks for any level 
  34. $levels = false; 
  35.  
  36. //check their level 
  37. if(pmpro_hasMembershipLevel($levels)) 
  38. $hasaccess = true; 
  39.  
  40. //is there a delay? 
  41. if($hasaccess && !empty($delay)) 
  42. {  
  43. //okay, this post requires membership. start by getting the user's startdate 
  44. if(!empty($levels)) 
  45. $sqlQuery = "SELECT UNIX_TIMESTAMP(startdate) FROM $wpdb->pmpro_memberships_users WHERE status = 'active' AND membership_id IN(" . implode(", ", $levels) . ") AND user_id = '" . $current_user->ID . "'ORDERBY id LIMIT 1"; 
  46. else 
  47. $sqlQuery = "SELECT UNIX_TIMESTAMP(startdate) FROM $wpdb->pmpro_memberships_users WHERE status = 'active' AND user_id = '" . $current_user->ID . "'ORDERBY id LIMIT 1"; 
  48.  
  49. $startdate = $wpdb->get_var($sqlQuery); 
  50.  
  51. //adjust start date to 12AM 
  52. $startdate = strtotime(date_i18n("Y-m-d", $startdate)); 
  53.  
  54. if(empty($startdate)) 
  55. //user doesn't have an active membership level 
  56. $hasaccess = false; 
  57. else 
  58. //how many days has this user been a member? 
  59. $now = current_time('timestamp'); 
  60. $days = ($now - $startdate)/3600/24; 
  61.  
  62. if($days < intval($delay)) 
  63. $hasaccess = false; //they haven't been around long enough yet 
  64.  
  65. //to show or not to show 
  66. if($hasaccess) 
  67. return do_shortcode($content); //show content 
  68. else 
  69. return ""; //just hide it