Today I figured out how to let people use their email to login instead of their username to the WordPress driven site called Jacked Pack. I had already installed the WP-Members plugin in order to add custom registration fields to the WordPress account page (for the “HUGEness profile”), but when I tried installing the WP Email Login plugin it didn’t have any effect. I figured this was because the WP-Members plugin already supercedes the regular login functionality, so I asked Chad Butler (who created the WP-Members plugin) about the issue.
Fortunately Chad had already created a way for people to add plugins to his plugin, so that’s what I ended up doing in this case. I followed his instructions by creating a file called wp-members-pluggable.php in my /wp-content/plugins directory and copying the wpmem-login function into it (I found that function in the wp-members-core.php file in the wp-members plugin folder). Then I added my own simple function (which was taken from the WP Email Login plugin) to get the username from an email address in case someone enters an email in the username field. Then I added just one line to the wpmem-login function which calls my function. Once I got that working, I copied the wpmem_inc_login function (which I found in the wp-members-dialogs.php file in the wp-members plugin folder) into my wp-members-pluggable.php file and changed one line to tell people they can use Username or Email in the first field.
See below for my complete plugin for the WP-Members WordPress Plugin, or download it here:
<?php
/**
* WP-Members Pluggable Functions
*
* These functions replace those in the wp-members plugin
*
*/
if( ! function_exists( 'wpmem_login' ) ):
/**
* Logs in the user
*
* Logs in the the user using wp_signon (since 2.5.2). If login
* is successful, it redirects and exits; otherwise "loginfailed"
* is returned.
*
* @since 0.1
*
* @uses apply_filters Calls 'wpmem_login_redirect' hook to get $redirect_to
*
* @uses wp_signon
* @uses wp_redirect Redirects to $redirect_to if login is successful
* @return string Returns "loginfailed" if the login fails
*/
function wpmem_login()
{
if( isset( $_POST['redirect_to'] ) ) {
$redirect_to = $_POST['redirect_to'];
} else {
$redirect_to = $_SERVER['PHP_SELF'];
}
$redirect_to = apply_filters( 'wpmem_login_redirect', $redirect_to );
if( isset( $_POST['rememberme'] ) == 'forever' ) {
$rememberme = true;
} else {
$rememberme = false;
}
if( $_POST['log'] &amp;amp;amp;amp;&amp;amp;amp;amp; $_POST['pwd'] ) {
$user_login = sanitize_user( $_POST['log'] );
$user_login = wpmem_login_check_for_email($user_login);
$creds = array();
$creds['user_login'] = $user_login;
$creds['user_password'] = $_POST['pwd'];
$creds['remember'] = $rememberme;
$user = wp_signon( $creds, false );
if( ! is_wp_error( $user ) ) {
if( ! $using_cookie )
wp_setcookie( $user_login, $user_pass, false, '', '', $rememberme );
wp_redirect( $redirect_to );
exit();
} else {
return "loginfailed";
}
} else {
//login failed
return "loginfailed";
}
} // end of login function
endif;
if ( ! function_exists( 'wpmem_login_check_for_email' ) ):
/**
* Get Username from Email
*
* Takes the username and checks if there is a user with an email that matches.
* If there is, the username associated with the email is returned.
* Otherwise the username is returned as it was passed in.
*
* @uses get_user_by
* @return string Returns username associated with email or the same
* username that was passed in
*/
function wpmem_login_check_for_email($username) {
if ( !empty( $username ) )
$user = get_user_by( 'email', $username );
if ( isset( $user->user_login, $user ) )
$username = $user->user_login;
return $username;
} // end of check for email in the username
endif;
if ( ! function_exists( 'wpmem_inc_login' ) ):
/**
* Login Dialog
*
* Loads the login form for user login
*
* @since 1.8
*
* @uses apply_filters Calls wpmem_restricted_msg filters message content
* @uses wpmem_login_form()
*
* @param string $page
* @return string the generated html for the login form
*/
function wpmem_inc_login( $page="page" )
{
global $wpmem_regchk;
$str = '';
$arr = get_option( 'wpmembers_dialogs' );
if( $page == "page" ){
if( $wpmem_regchk!="success" ){
// this shown above blocked content
$str = '<p>' . stripslashes($arr[0]) . '</p>';
// filter blocked content message
$str = apply_filters( 'wpmem_restricted_msg', $str );
}
}
//$arr = array( __( 'Existing users Login', 'wp-members' ), __( 'Username', 'wp-members' ), 'text', 'log', __( 'Password', 'wp-members' ), 'password', 'pwd', 'login', __( 'Login', 'wp-members' ), 'username', 'password' );
$arr = array( __( 'Existing users Login', 'wp-members' ), __( 'Username or Email', 'wp-members' ), 'text', 'log', __( 'Password', 'wp-members' ), 'password', 'pwd', 'login', __( 'Login', 'wp-members' ), 'username', 'password' );
$str = $str . wpmem_login_form( $page, $arr );
return $str;
}
endif;
?>
Update July 5th, 2013
I was asked about the registration function as well (see comments from Brian Weiss below) and although I haven’t made a plugin for the WP-Members registration function I have done auto registrations in a separate file. I tried posting code in a comment and it didn’t turn out well, so here is the relevant code I used (in a separate php file I created in the wordpress directory) to auto create wordpress accounts based on their email and firstname (you could do it without using their name if you want, since the users will never need to know their username). If someone puts this into a plugin for the WP-Members registration function please let me know and I’ll link to it so others who want to do that can see the solution.
#Initial try at an unclaimed username
$username = $firstName.rand(1,1000);
#Now create a WP account
include('wp-config.php');
// Initialize connection and select database
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
or die('Could not connect: ' . mysql_error());
//echo 'Connected successfully';
mysql_select_db(DB_NAME) or die('Could not select database');
//echo "connected to db<br>";
#WP Username has to be unique
while (username_exists( $username )) {
$username = $firstName.rand(1,1000);
}
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
//echo "about to create user $username with pwd $random_password and email $email<br>";
$user_id = wp_create_user( $username, $random_password, $email );
if (is_int($user_id)) {
//echo "new user id is $user_id<br>";
} else {
if(is_wp_error($user_id)) { echo $user_id->get_error_message(); }
exit;
}
// then send a welcome email and/or redirect wherever you want...