Last active
August 3, 2021 12:31
-
-
Save femiyb/909b8d4a75e7c53519e73ed7a22a46ff to your computer and use it in GitHub Desktop.
Assign a LEVEL to a unique role with Expiration Date
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 | |
/** | |
* Assign a LEVEL to a unique role with Expiration Date | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function assign_pmpro_level_to_role($user_id, $role, $old_roles) | |
{ | |
//we found a role related to pmpro level | |
if($role == "shop_manager") | |
{ | |
$changed_level = get_user_meta( $user_id, 'changed_level', true ); | |
if ( empty( $changed_level ) || $changed_level !== 2 ) { | |
$changed_level = array(); | |
$changed_level['user_id'] = $user_id; | |
$changed_level['membership_id'] = 2; | |
// Expiration Date | |
$changed_level['enddate'] = date( '2021-12-31 00:00:00'); | |
$changed = pmpro_changeMembershipLevel( $changed_level, $user_id ); | |
update_user_meta( $user_id, 'changed_level', 2 ); | |
} | |
else | |
delete_user_meta($user_id, 'changed_level'); | |
} | |
} | |
add_action('set_user_role', 'assign_pmpro_level_to_role', 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment