Skip to content

Instantly share code, notes, and snippets.

@JarrydLong
Last active August 14, 2024 14:00
Show Gist options
  • Save JarrydLong/cc80f9479869c291f1039fc3dba82417 to your computer and use it in GitHub Desktop.
Save JarrydLong/cc80f9479869c291f1039fc3dba82417 to your computer and use it in GitHub Desktop.
<?php
/**
* This recipe will prevent members from being able to login unless they have an active membership on your website.
*
* 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 );
@kimcoleman
Copy link

For more information about this code recipe, please read the full article here: https://www.paidmembershipspro.com/restrict-user-login-for-members-only/

@laurenhagan0306
Copy link

This recipe is included in the blog post on "Restrict User Login for Members Only" at Paid Memberships Pro here: https://www.paidmembershipspro.com/restrict-user-login-for-members-only/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment