Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/e7b108c0f949bb2db71186cab3a983c4 to your computer and use it in GitHub Desktop.
Save dwanjuki/e7b108c0f949bb2db71186cab3a983c4 to your computer and use it in GitHub Desktop.
Logs out non-approved members after PMPro checkout
<?php
/**
* This recipe logs out non-approved members after checkout.
*
* Requires PMPro Approvals - pmpro-approvals
* https://www.paidmembershipspro.com/add-ons/approval-process-membership/
*
* 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_non_approved_confirmation_url( $url, $user_id, $level_id) {
// only check logged in users.
if ( ! is_user_logged_in() ) {
return $url;
}
// bail if pmpro-approvals not active
if ( ! class_exists( 'PMPro_Approvals' ) ) {
return $url;
}
//check to see if the user has any membership level
if ( pmpro_hasMembershipLevel() ) {
// Get the current user ID and their level ID for the PMPro Approvals meta.
$level = pmpro_getMembershipLevelForUser( $user_id );
$level_id = $level->id;
// Bail if the level does not require approval
if ( ! PMPro_Approvals::requiresApproval( $level_id ) ) {
return $url;
}
//get the user meta.
$approval_status = get_user_meta( $user_id, 'pmpro_approval_' . $level_id, true );
//check the user meta.
if ( 'pending' == $approval_status['status'] || 'denied' == $approval_status['status'] ) {
// Add querystring to confirmation page url
$url = add_query_arg( 'pending_approval', 1, $url );
}
}
return $url;
}
add_filter( 'pmpro_confirmation_url', 'my_pmpro_non_approved_confirmation_url', 10, 3 );
function my_pmpro_log_out_non_approved() {
global $pmpro_pages;
if ( empty( $pmpro_pages ) || empty( $pmpro_pages['confirmation'] ) ) {
return;
}
if( is_page( $pmpro_pages['confirmation'] ) && isset( $_GET['pending_approval'] ) ) {
wp_logout();
$redirect_url = site_url( '/pending-approval/' ); // redirect non-approved here after checkout
wp_redirect( $redirect_url );
exit;
}
}
add_action( 'template_redirect', 'my_pmpro_log_out_non_approved' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment