Skip to content

Instantly share code, notes, and snippets.

@MaximilianoRicoTabo
Last active October 18, 2023 13:34
Show Gist options
  • Save MaximilianoRicoTabo/48ab01900384e54f027aacfc0c7130eb to your computer and use it in GitHub Desktop.
Save MaximilianoRicoTabo/48ab01900384e54f027aacfc0c7130eb to your computer and use it in GitHub Desktop.
show hidden levels in special conditions
<?php
/**
* Bring back hidden levels for those users currently having that level or they had recently.
* $hidden_level is the id of the level to bring back.
* $end_date_to_compare is the date to compare to determine how much time ago the user had the level.
*
* Add this code to your site by following this guide - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function adjusting_the_pmpro_levels_array( $levels ) {
$current_user = wp_get_current_user();
// fetch levels again but bringing hidden.
$levels = pmpro_getAllLevels(true, true);
$new_levels = array();
// 2 is the hidden level, change the id with the one need to hide and make available for certain users.
$hidden_level = "2";
foreach ( $levels as $level ) {
if($level->id === $hidden_level ) {
global $wpdb;
$user_level = pmpro_getSpecificMembershipLevelForUser( $current_user->ID, $level->id );
$has_level = ! empty( $user_level );
//compare date to determine how much time ago the user had the level.
$end_date_to_compare = date("Y-m-d h:m", strtotime("-1 month"));
$had_level = $user_id = $wpdb->get_row( $wpdb->prepare( "
SELECT membership_id
FROM $wpdb->pmpro_memberships_users
WHERE user_id = %d
AND membership_id = %d
AND enddate > %s",
array( $current_user->ID, $hidden_level, $end_date_to_compare )
) );
if ( $has_level || $had_level ) {
$new_levels[] = $level;
}
} else {
$new_levels[] = $level;
}
}
return $new_levels;
}
add_filter( 'pmpro_levels_array', 'adjusting_the_pmpro_levels_array' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment