Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Login-Logout Folder and 2 restrict login recipes #219

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions login-logout/require-confirmed-email-address-to-login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Require Confirmed Email Address to Log In
*
* title: Require Confirmed Email Address to Log In
* layout: snippet
* collection: login-logout
* category: login
* link: https://www.paidmembershipspro.com/restrict-user-login-for-members-only/#h-code-recipe-1-restrict-user-login-to-active-members
*
* 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_check_login( $user, $password ) {

$validated = get_user_meta( $user->ID, "pmpro_email_confirmation_key", true );

if( $validated != 'validated' && !empty( $validated ) ) {
return new WP_Error( 'user_not_verified', 'User has not validated their email' );
}

return $user;
}

add_filter( 'wp_authenticate_user', 'my_pmpro_check_login', 10, 2 );
42 changes: 42 additions & 0 deletions login-logout/restrict-user-login-to-active-members.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Prevent members from being able to login unless they have an active membership
*
* title: Restrict User Login to Active Members
* layout: snippet
* collection: login-logout
* category: login
* link:https://www.paidmembershipspro.com/restrict-user-login-for-members-only/#h-code-recipe-1-restrict-user-login-to-active-members
*
* 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 mypmpro_validate_membership_login( $user, $username, $password ){

if( !function_exists( 'pmpro_getMembershipLevelsForUser' ) ){
return $user;
}

$user = get_user_by( 'login', $username );

if( empty( $user ) ) {
return;
}

if( in_array( 'administrator', $user->roles ) ){
return $user;
}

$membership_levels = pmpro_getMembershipLevelsForUser( $user->ID );

if( empty( $membership_levels ) ){ //No membership level assigned - no login allowed
return;
}

return $user;

}
add_filter( 'authenticate', 'mypmpro_validate_membership_login', 99, 3 );