Created
September 29, 2020 05:43
-
-
Save ipokkel/2d611409302dc835714b3c2f436d3743 to your computer and use it in GitHub Desktop.
Add custom user meta on level change #pmpro
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 | |
/** | |
* This recipe adds user meta from the options table | |
* for the user if the user has a membership level. | |
* | |
* 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 dctit_add_cp_based_on_level( $level_id, $user_id ) { | |
$level = pmpro_getMembershipLevelForUser( $user_id ); | |
if ( 0 === $level_id || ! $level ) { | |
// clear meta value for meta key when user does not have a membership level/cancels | |
update_user_meta( $user_id, 'user_cp_dct', '' ); | |
} else { | |
$l_name = strtolower( $level->name ); | |
$l_id = $level->id; | |
$l_key = str_replace( ' ', '_', $l_name ) . $l_id; | |
$level_cp = get_option( $l_key ); | |
// update meta key if we have a meta value | |
if ( ! empty( $level_cp ) ) { | |
update_user_meta( $user_id, 'user_cp_dct', $level_cp ); | |
} | |
} | |
} | |
add_action( 'pmpro_after_change_membership_level', 'dctit_add_cp_based_on_level', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment