Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JarrydLong/ffb1bd62634d577cfb072a0c878a249c to your computer and use it in GitHub Desktop.
Save JarrydLong/ffb1bd62634d577cfb072a0c878a249c to your computer and use it in GitHub Desktop.
Stop non-approved users from logging in.
<?php
/**
* This is an example to stop non-approved user's from logging in using the Approvals Add On For Paid Memberships Pro.
* Add this code below to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_stop_users_logging_in( $user, $password ) {
$user_id = $user->ID;
if( !empty( $user_id ) ) {
$user_registered = strtotime( $user->user_registered );
//All users that registered before 2024-01-16 23:59:59 will automatically be given access
if( $user_registered < strtotime( '2024-01-16 23:59:59' ) ) {
return $user;
}
$level = pmpro_getMembershipLevelForUser( $user_id );
if( function_exists( 'pmproec_isEmailConfirmationLevel' ) ) {
$is_confirmation = pmproec_isEmailConfirmationLevel( $level->ID );
$validation_key = get_user_meta($user_id, "pmpro_email_confirmation_key", true);
if ( ! empty( $validation_key ) || $validation_key !== 'validated' ) {
$access = false;
} else {
$access = true;
}
if ( ! $access ) {
return new WP_Error( 'not_approved', __( 'Your account is not approved yet.', 'pmpro' ) );
}
}
// Get approval status
if( class_exists( 'PMPro_Approvals') ) {
$approval = PMPro_Approvals::getUserApproval( $user_id );
// User currently does not need approval.
if ( empty( $approval ) ) {
return $user;
}
$approval_status = $approval['status'];
if ( ( ! empty( $approval_status ) && $approval_status != 'approved' ) ) {
$access = false;
} else {
$access = true;
}
}
}
if ( ! $access ) {
return new WP_Error( 'not_approved', __( 'Your account is not approved yet.', 'pmpro' ) );
}
return $user;
}
add_filter( 'wp_authenticate_user', 'my_pmpro_stop_users_logging_in', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment