Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Add a Grace Period to a Membership Level with PMPro #223

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions membership-levels/add-grace-to-membership-level.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* Add a 15-Day Grace Period to a Membership Level with PMPro
*
* url: https://www.paidmembershipspro.com/add-a-grace-period-to-a-pmpro-membership/
*
* Replace `15` on line 29 with your preferred number of days
*
* title: Add a Grace Period to a Membership Level
* layout: snippet
* collection: membership-levels
* category: grace
*
* 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_membership_post_membership_expiry( $user_id, $level_id ) {
// Make sure we aren't already in a grace period for this level
$grace_level = get_user_meta( $user_id, 'grace_level', true );
if ( empty( $grace_level ) || $grace_level !== $level_id ) {
// Give them their level back with 15 day expiration
$grace_level = array();
$grace_level['user_id'] = $user_id;
$grace_level['membership_id'] = $level_id;
$grace_level['enddate'] = date( 'Y-m-d H:i:s', strtotime( '+15 days', current_time( 'timestamp' ) ) );
$changed = pmpro_changeMembershipLevel( $grace_level, $user_id );
update_user_meta( $user_id, 'grace_level', $level_id );
}
else
delete_user_meta($user_id, 'grace_level');
}

add_action('pmpro_membership_post_membership_expiry', 'my_pmpro_membership_post_membership_expiry', 10, 2);