Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kimcoleman/0111cfd3cb8c0d19f44ebfb67ab56199 to your computer and use it in GitHub Desktop.
Save kimcoleman/0111cfd3cb8c0d19f44ebfb67ab56199 to your computer and use it in GitHub Desktop.
UNTESTED: Check if a user is already at level 1. If they have been at that level for 30 days, upgrade them to level 2.
<?php
/**
* UNTESTED: Check if a user is already at level 1. If they have been at that level for 30 days, upgrade them to level 2.
*
* 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 my_pmpro_check_and_upgrade_level_on_login( $user_login, $user ) {
// Set the membership levels
$level_1 = 1; // Level 1 ID
$level_2 = 2; // Level 2 ID
$user_id = $user->ID;
// Get current membership levels for the user
$current_levels = pmpro_getMembershipLevelsForUser( $user_id );
// Check if the user is at level 1
if ( isset( $current_levels[ $level_1 ] ) ) {
// Check if the user has been at level 1 for 30 days
$startdate = $current_levels[ $level_1 ]->startdate;
// Calculate the time difference in days
$days_at_level_1 = ( current_time( 'timestamp' ) - strtotime( $startdate ) ) / DAY_IN_SECONDS;
if ( $days_at_level_1 >= 30 ) {
pmpro_changeMembershipLevel( $level_2, $user_id );
}
}
}
add_action( 'wp_login', 'my_pmpro_check_and_upgrade_level_on_login', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment