Forked from strangerstudios/pmpro-member-count-shortcode.php
Created
March 15, 2021 12:45
-
-
Save JarrydLong/4ef07d8ab045f18b9e4e0987b2ccf0d6 to your computer and use it in GitHub Desktop.
PMPro Member Count Shortcode
This file contains hidden or 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 <[email protected]>, 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` IN (" . $attrs['level'].")"; | |
} | |
$member_count = $wpdb->get_var($sql); | |
if (!is_wp_error($member_count)) { | |
if (! empty($attrs['level'])) { | |
if(!empty($attrs['justnumber'])) | |
return $member_count; | |
else | |
return sprintf( __( "This site has %d members", "pmpromsc" ), $member_count ); | |
} 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