Forked from JarrydLong/mypmpro-set-next-payment-date-expiration-date.php
Last active
April 22, 2025 13:56
-
-
Save kimwhite/252f87ec007455e92e53f05251ffc4e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //do not copy | |
/** | |
* Gets the next payment date and marks that as the expiration date for users. | |
* | |
* The "next payment" value is an estimate based on the billing cycle of the subscription and the last order date. It may be off from the actual recurring date set at the gateway, especially if the subscription was updated at the gateway. | |
* | |
* Remove the // on line 40 to run the update query. | |
* | |
* Add /wp-admin/?set_expiration_dates=true to your URL to run. | |
* | |
* 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 mypmpro_set_expiration_dates() { | |
if ( isset( $_REQUEST['set_expiration_dates'] ) ) { | |
global $wpdb; | |
$users = $wpdb->get_results( "SELECT * FROM $wpdb->pmpro_memberships_users WHERE `status` = 'active'" ); | |
if ( $users ) { | |
foreach( $users as $user ) { | |
$next = pmpro_next_payment( $user->user_id, array( 'success', 'cancelled', '' ) ); | |
if( $next !== FALSE ) { | |
echo "User ".$user->user_id." will expire on ".date( 'Y-m-d H:i:s', $next )."<br/>"; | |
//They will renew and we need to set an expiration date | |
$sqlQuery = "UPDATE $wpdb->pmpro_memberships_users SET enddate = '".date( 'Y-m-d 00:01:01', $next )."' WHERE user_id = '".$user->user_id."'"; | |
/** | |
* Remove the comment for the next line when you're sure that the | |
* expiration dates match up for your users | |
*/ | |
// $wpdb->query( $sqlQuery ); | |
} | |
} | |
exit(); | |
} | |
} | |
} | |
add_action( 'init', 'mypmpro_set_expiration_dates' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment