Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Created September 29, 2020 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/2d611409302dc835714b3c2f436d3743 to your computer and use it in GitHub Desktop.
Save ipokkel/2d611409302dc835714b3c2f436d3743 to your computer and use it in GitHub Desktop.
Add custom user meta on level change #pmpro
<?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