Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Assign a LEVEL to a unique role with Expiration Date
<?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