Skip to content

Instantly share code, notes, and snippets.

@michaelbeil
Created January 4, 2021 09:37
Show Gist options
  • Save michaelbeil/6291ba60777800c7b1b45526a4bbb2cc to your computer and use it in GitHub Desktop.
Save michaelbeil/6291ba60777800c7b1b45526a4bbb2cc to your computer and use it in GitHub Desktop.
PMPro Member Count Shortcode
<?php
/*
Plugin Name: PMPro Member Count Shortcode
Plugin URI: https://www.paidmembershipspro.com/wp/pmpro-customizations/
Description: Show the number of members in a level w/a status
Version: .1
Author: Stranger Studios
Author URI: https://www.strangerstudios.com
*/
function pmpro_member_count_shortcode( $attrs = null )
{
global $wpdb;
$status = null;
$level = null;
$attrs = shortcode_atts(
array(
'status' => 'active',
'level' => null,
),
$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 <code>status</code> IN ('" . implode("', '", $statuses) . "')";
if (! empty($attrs['level'])) {
$sql .= "
AND <code>membership_id</code> = " . intval($attrs['level']);
}
$member_count = $wpdb->get_var($sql);
if (!is_wp_error($member_count)) {
if (! empty($attrs['level'])) {
$l = pmpro_getLevel($attrs['level']);
return sprintf( __( "This site has %d %s members", "pmpromsc" ), $member_count, $l->name );
} 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