Last active
April 13, 2021 04:32
-
-
Save strangerstudios/4743bc5d636e875b0815952dafe7677a to your computer and use it in GitHub Desktop.
PMPro Member Count Shortcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: PMPro Member Count Shortcode | |
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-customizations/ | |
Description: Show the number of members in a level w/a status | |
Version: .2 | |
Author: Thomas Sjolshagen @ Stranger Studios <thomas@eighty20results.com>, strangerstudios | |
Author URI: http://www.strangerstudios.com | |
*/ | |
function pmpro_member_count_shortcode( $attrs = null ) | |
{ | |
global $wpdb; | |
$attrs = shortcode_atts( | |
array( | |
'status' => 'active', | |
'level' => null, | |
'justnumber' => false, | |
), | |
$attrs | |
); | |
$statuses = array_map('trim', explode(',', $attrs['status'])); | |
if (! is_array($statuses) && ! empty($attrs['status']) ) { | |
$statuses = array($attrs['status']); | |
} | |
$sql = "SELECT COUNT(*) | |
FROM {$wpdb->pmpro_memberships_users} | |
WHERE `status` IN ('" . implode("', '", $statuses) . "')"; | |
if (! empty($attrs['level'])) { | |
$sql .= " | |
AND `membership_id` = " . intval($attrs['level']); | |
} | |
$member_count = $wpdb->get_var($sql); | |
if (!is_wp_error($member_count)) { | |
if (! empty($attrs['level'])) { | |
$l = pmpro_getLevel($attrs['level']); | |
if(!empty($attrs['justnumber'])) | |
return $member_count; | |
else | |
return sprintf( __( "This site has %d %s members", "pmpromsc" ), $member_count, $l->name ); | |
} else { | |
if(!empty($attrs['justnumber'])) | |
return $member_count; | |
else | |
return sprintf( __( "This site has %d members", "pmpromsc" ), $member_count ); | |
} | |
} else { | |
return sprintf( __( "Error while processing request: %s", "pmpromsc" ), $wpdb->print_error() ); | |
} | |
} | |
add_shortcode('pmpro_member_count', 'pmpro_member_count_shortcode'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This recipe is included in the blog post on "Display a Count of Members by Level and/or Status via Shortcode" at Paid Memberships Pro here: https://www.paidmembershipspro.com/display-count-members-level-andor-status-via-shortcode/