Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save strangerstudios/9936197 to your computer and use it in GitHub Desktop.
Save strangerstudios/9936197 to your computer and use it in GitHub Desktop.
Only allow users to use the trial level once with Paid Memberships Pro.
<?php
/*
Only allow users to use the trial level once.
Add this code to your active theme's functions.php
or a custom plugin.
Be sure to change the $trial_level_id variable in multiple places.
*/
//record when users gain the trial level
function my_pmpro_after_change_membership_level($level_id, $user_id)
{
//set this to the id of your trial level
$trial_level_id = 8;
if($level_id == $trial_level_id)
{
//add user meta to record the fact that this user has had this level before
update_user_meta($user_id, "pmpro_trial_level_used", "1");
}
}
add_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 10, 2);
//check at checkout if the user has used the trial level already
function my_pmpro_registration_checks($value)
{
global $current_user;
//set this to the id of your trial level
$trial_level_id = 8;
if($current_user->ID && intval($_REQUEST['level']) == $trial_level_id)
{
//check if the current user has already used the trial level
$already = get_user_meta($current_user->ID, "pmpro_trial_level_used", true);
//yup, don't let them checkout
if($already)
{
global $pmpro_msg, $pmpro_msgt;
$pmpro_msg = "You have already used up your trial membership. Please select a full membership to checkout.";
$pmpro_msgt = "pmpro_error";
$value = false;
}
}
return $value;
}
add_filter("pmpro_registration_checks", "my_pmpro_registration_checks");
//swap the expiration text if the user has used the trial
function my_pmpro_level_expiration_text($text, $level)
{
global $current_user;
//set this to the id of your trial level
$trial_level_id = 8;
if($current_user->ID && $level->id == $trial_level_id)
{
$text = "You have already used up your trial membership. Please select a full membership to checkout.";
}
return $text;
}
add_filter("pmpro_level_expiration_text", "my_pmpro_level_expiration_text", 10, 2);
?>
@ESGC6E
Copy link

ESGC6E commented Oct 25, 2016

Hi,
Could I amend this slightly, on line 39, so that the text "full membership" is a href to an external page?

@havasuscannerfeed
Copy link

Is there a way to disable the submit button? While testing, I see the text comes up - BUT - they are still able to click and proceed through to the member-checkout screen.

@havasuscannerfeed
Copy link

I had to remove this code because it was showing to EVERY user who was on the trial signup regardless of them not having tried the trial before.

@hermleke
Copy link

C'est quoi la solution en fin de compte
Le bouton renouveller est toujours visible même avec ce code

@Diambere19
Copy link

Diambere19 commented Jan 14, 2021

C'est quoi la solution en fin de compte
Le bouton renouveller est toujours visible même avec ce code

Effectivement le code ne fonctionne pas car on peut quand même y souscrire.
Regarde la révision de @itsjusteileen. Le bouton renouveler est toujours visible mais lorsque tu veux valider ta souscription il te met un message d'erreur te disant que tu as déja utilisé ce niveau.
https://gist.github.com/itsjusteileen/0c5077e8175eeeb6bb362c975b1f872b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment