Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/2623bac08c883662980be5a80499d010 to your computer and use it in GitHub Desktop.
Save dwanjuki/2623bac08c883662980be5a80499d010 to your computer and use it in GitHub Desktop.
Stop non-approved members from logging in and display a message when using the Approvals Add On
<?php
/**
* Stop non-approved members from logging in and display a message when using the Approvals Add On
*
* 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_stop_non_approved_members_logging_in( $user, $username, $password ) {
// Bail if PMPro and PMPro Approvals aren't active.
if ( ! function_exists( 'pmpro_getMembershipLevelForUser' ) || ! class_exists( 'PMPro_Approvals' ) ) {
return $user;
}
if( $user instanceof WP_User ) {
$user_id = $user->ID;
$level = pmpro_getMembershipLevelForUser( $user_id );
// Get approval status
$approval = PMPro_Approvals::getUserApproval( $user_id );
if ( ! empty( $approval['status'] ) && $approval['status'] != 'approved' ) {
// Display error on WP default login page if member is not approved
return new WP_Error( 'pending_approval', 'Your membership has not yet been approved.' );
}
}
return $user;
}
add_filter( 'authenticate', 'my_pmpro_stop_non_approved_members_logging_in', 40, 3 );
function my_pmpro_login_pending_approval_message( $content ) {
if( isset( $_GET['action'] ) && $_GET['action'] == 'pending_approval' ) {
// Display alert on PMPro login page if member is not approved
$html = '<div class="pmpro_message pmpro_alert">Your membership has not yet been approved.</div>';
$content = $html . $content;
}
return $content;
}
add_filter( 'login_form_top', 'my_pmpro_login_pending_approval_message' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment