Forked from strangerstudios/my_init_pmpro_mini_api.php
Last active
April 3, 2021 03:51
-
-
Save kimcoleman/af286d76e0ed80e4732d624830ebaf4e to your computer and use it in GitHub Desktop.
Very basic custom RESTful API to check the membership level of a Paid Memberships Pro User
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 | |
/** | |
* Call to http://yoursite.com/[email protected]&secret=SOMESECRETHERE to check the membership level of a user. | |
*/ | |
function my_init_pmpro_mini_api() { | |
if ( function_exists( 'pmpro_getMembershipLevelForUser' ) && | |
! empty( $_REQUEST[ 'verify' ] ) && | |
! empty( $_REQUEST[ 'secret' ] ) ) | |
{ | |
if ( $_REQUEST[ 'secret' ] != 'SOMESECRETHERE' ) { | |
wp_die( 'Invalid secret.' ); | |
} | |
$user = get_user_by( 'email', str_replace( ' ', '+', ( $_REQUEST['verify'] ) ) ) ; | |
if ( empty( $user ) ) { | |
wp_die( 'User not found.' ); | |
} | |
$membership_level = pmpro_getMembershipLevelForUser( $user->ID ); | |
if ( empty( $membership_level ) ) { | |
die( '0' ); | |
} | |
//If User and membership level found, output json encoded membership level info. | |
echo json_encode( $membership_level ); | |
exit; | |
} | |
} | |
add_action( 'init', 'my_init_pmpro_mini_api' ); |
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 "Interacting with Paid Memberships Pro Through APIs" at Paid Memberships Pro here: https://www.paidmembershipspro.com/interacting-with-paid-memberships-pro-through-apis/