Forked from andrewlimaza/pmpro-approvals-login-example.php
Last active
February 22, 2024 22:20
-
-
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
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 | |
/** | |
* 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