Skip to content

Instantly share code, notes, and snippets.

@fovoc
Created December 20, 2016 19:27
Show Gist options
  • Save fovoc/d9ee6943942a6f094b213e8a4a80c5f8 to your computer and use it in GitHub Desktop.
Save fovoc/d9ee6943942a6f094b213e8a4a80c5f8 to your computer and use it in GitHub Desktop.
WPMUDEV M2PRO API theme use example
<?php
/**
* If you want to use M2 API in theme's functions.php try the following method.
* Calling ms_init hook will work from plugins and mu-plugins, as this hook is not designed to work from a theme.
*/
add_action( 'init', 'my_api_hook', 99, 1 );
function my_api_hook() {
// Assume user ID is 3
$user_id = 3;
$api = ms_api();
$member = $api->get_member( $user_id );
$subscriptions = $member->subscriptions;
// If the member has only one subscription, you are sure about that.
// Otherwise go with a loop
$subscription = $subscriptions[0];
// Start Date & End Date
m2echo( 'Start Date', $subscription->start_date );
m2echo( 'Expire Date', $subscription->expire_date );
// Admin test
$admin_test = MS_Model_Member::is_admin_user( $user_id );
// Will be introduced in 1.0.2.8
//$admin_test = $api->is_admin_user( $user_id );
if( $admin_test ) echo 'User is an admin.<br><br>';
else echo 'User is not an admin.<br><br>';
// Get all memnberships
$memberships = $api->list_memberships();
echo "<pre>";
//print_r($memberships);
echo "</pre>";
// Current user membership
$current_membership = $api->detect_membership();
echo "<pre>";
//print_r($current_membership);
echo "</pre>";
}
function m2echo( $label, $data ) {
echo $label . ': ' . $data . '<br><br>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment