Created
April 9, 2014 17:29
-
-
Save supercleanse/10294791 to your computer and use it in GitHub Desktop.
Updating roles via a lookup array when MemberPress Transaction completes
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: MemberPress Products to User Roles | |
Plugin URI: http://memberpress.com | |
Description: Assigns a member to a User Role depending on what Product they purchased. | |
Version: 1.0.0 | |
Author: Caseproof, LLC | |
Author URI: http://caseproof.com | |
Copyright: 2004-2013, Caseproof, LLC | |
*/ | |
function memberpress_user_role($txn) | |
{ | |
$user = get_user_by('id', $txn->user_id); | |
//user doesn't exist for some reason, or is an admin - we don't want to wipe out the admin's role haha | |
if($user === false || user_can($txn->user_id, 'manage_options')) | |
return; | |
$role_ids = json_decode('{ | |
"bronze": [2,5,8], | |
"silver": [3,6], | |
"gold": [4,7], | |
"platinum": 9 | |
}'); | |
foreach( $role_ids as $role => $ids ) { | |
if( !is_array($ids) ) { $ids = array($ids); } | |
if( in_array($txn->product_id, $ids) ) { | |
return $user->set_role($role); | |
} | |
} | |
} | |
add_action('mepr-txn-status-complete', 'memberpress_user_role'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Blair, this works great. Is there a way to modify the code to remove a person from a role or reset them to another one, upon a transaction, subscription expiration? I found your other code related to doing something when mepr-event-transaction-expired, but i can't seem to get it to work the other direction :)