Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ipokkel/ecd733d8522171593e0703f1092816e7 to your computer and use it in GitHub Desktop.
Save ipokkel/ecd733d8522171593e0703f1092816e7 to your computer and use it in GitHub Desktop.
<?php
/**
* Cancel membership level and subscription at payment gateway when a member is denied.
*
* 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_cancel_subscription_on_denial( $user_id, $level_id ) {
// Get the user's order.
$order = new MemberOrder();
$order->getLastMemberOrder( $user_id, 'success', $level_id );
// Cancel the membership level, which will also cancel any subscriptions.
if ( ! empty( $order ) && ! empty( $order->id ) ) {
pmpro_cancelMembershipLevel( $level_id, $user_id, 'denied' );
}
}
add_action( 'pmpro_approvals_after_deny_member', 'my_pmpro_cancel_subscription_on_denial', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment