Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active January 24, 2022 03:53
Show Gist options
  • Save strangerstudios/cd8798839f7a878813fea5119ec2f8ce to your computer and use it in GitHub Desktop.
Save strangerstudios/cd8798839f7a878813fea5119ec2f8ce to your computer and use it in GitHub Desktop.
Add a [haspaid] shortcode to use with Paid Memberships Pro.
/*
Function and shortcode to detect users with or without paid invoices.
Add this code to a custom plugin.
*/
//check if a user ever paid
function my_hasPaid($user_id = NULL, $level_id = NULL) {
global $current_user, $wpdb;
//make sure PMPro is active
if(!isset($wpdb->pmpro_membership_orders))
return false;
//no user passed? default to current user
if(empty($user_id))
$user_id = $current_user->ID;
//no user?
if(empty($user_id))
return false;
//figure out if we are in a live or test gateway_environment
$environment = pmpro_getOption('gateway_environment');
//query to check
$sqlQuery = "SELECT COUNT(*) FROM $wpdb->pmpro_membership_orders WHERE user_id = '" . esc_sql($user_id) . "' AND gateway_environment = '" . esc_sql($environment) . "' AND total > 0 AND status NOT IN('error', 'refund', 'token', 'review') ";
if(!empty($level_id))
$sqlQuery .= "AND membership_id = '" . esc_sql($level_id) . "' LIMIT 1";
//get val
$paid = $wpdb->get_var($sqlQuery);
//force true/false
return (bool)$paid;
}
/*
Shortcode using the my_hasPaid function.
[haspaid]This will show up if the user has paid for any level.[/haspaid]
[haspaid paid='0']This will show up if the user has NOT paid for any level.[/haspaid]
[haspaid paid='1' level='1']This will show up if the user has paid for level 1 specifically.[/haspaid]
[haspaid paid='0' level='1']This will show up if the user has not paid for level 1 specifically.[/haspaid]
*/
function my_haspaid_shortcode($atts, $content=null, $code="")
{
// $atts ::= array of attributes
// $content ::= text within enclosing form of shortcode element
// $code ::= the shortcode found, when == callback name
// examples: [haspaid level="3"]...[/haspaid]
extract(shortcode_atts(array(
'paid' => true,
'level' => NULL,
), $atts));
//convert paid attribute to bool
if($paid === '0' || $paid === 'false')
$paid = false;
else
$paid = true;
global $current_user;
//to show or not to show
if(my_hasPaid($current_user->ID, $level)) {
//return content if paid
if($paid)
return do_shortcode($content); //show content
else
return false;
} else {
//return content if NOT paid
if(!$paid)
return do_shortcode($content); //show content
else
return ""; //just hide it
}
}
add_shortcode("haspaid", "my_haspaid_shortcode");
@laurenhagan0306
Copy link

This recipe is included in the blog post on "New [haspaid] Shortcode: Show Content to Paying (or not paying) Members Only" at Paid Memberships Pro here: https://www.paidmembershipspro.com/new-haspaid-shortcode-show-content-paying-not-paying-members/

@churongcon
Copy link

Hello I think you should change query to
$sqlQuery = "SELECT COUNT(*) FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . esc_sql($user_id) . "' AND status = 'active' ";

It will work with membership dont have order and create by admin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment