Skip to content

Instantly share code, notes, and snippets.

@lucas-pelton
Last active December 25, 2015 16:59
Show Gist options
  • Save lucas-pelton/7010324 to your computer and use it in GitHub Desktop.
Save lucas-pelton/7010324 to your computer and use it in GitHub Desktop.
Functions to add to wp-members-pluggable.php to create a situation where you log in to Wordpress using email address instead of username.
<?php
/**
* WP-Members Pluggable Functions
*
* These functions replace those in the wp-members plugin
*
*/
add_filter( 'wpmem_inc_login_args', 'wpmem_inc_login_args_filter' );
if ( ! function_exists( 'wpmem_inc_login_args_filter' ) ):
/**
* Filter login form to change legend
*
*/
function wpmem_inc_login_args_filter( $args )
{
return array(
'heading' => __( 'Sign In', 'wp-members' ));
}
endif;
add_filter( 'wpmem_inc_login_inputs', 'wpmem_inc_login_inputs_filter' );
if ( ! function_exists( 'wpmem_inc_login_inputs_filter' ) ):
/**
* Filter login form to change username label
*
*/
function wpmem_inc_login_inputs_filter( $default_inputs )
{
$default_inputs[0]['name']= __( 'Email', 'wp-members' );
return $default_inputs;
}
endif;
add_filter( 'wpmem_inc_resetpassword_inputs', 'wpmem_resetpassword_form_filter' );
if ( ! function_exists( 'wpmem_resetpassword_form_filter' ) ):
/**
* Filter reset password form to show only a text box requesting email
*
*/
function wpmem_resetpassword_form_filter( $defaults )
{
return array(
array(
'name' => __('Email Address', 'wp-members'),
'type' => 'text',
'tag' => 'email',
'class' => 'password',
'div' => 'div_text'
)
);
}
endif;
add_filter( 'wpmem_pwdreset_args', 'wpmem_pwdreset_args_filter' );
if ( ! function_exists( 'wpmem_pwdreset_args_filter' ) ):
/**
* Filter password reset form submitted values to set username to the value entered for email address
*
*/
function wpmem_pwdreset_args_filter( $args )
{
$args['user']=$args['email'];
return $args;
}
endif;
add_filter('wpmem_register_data','wpmem_create_username_from_email');
if ( ! function_exists( 'wpmem_create_username_from_email' ) ):
/**
* Create Username from Email
*
* Creates a WP-friendly username from email address
* Address is alreay confirmed valid before filter is called
* [[[ is_email( $fields['user_email']) in wp-members-register.php/wpmem_registration ]]]
*
* @uses get_user_by
* @return array Returns filtered registration fields passed in from wp-members-register.php/wpmem_registration
*
*/
function wpmem_create_username_from_email($fields) {
$generated_username=sanitize_user($fields['user_email'],true);
if ($fields['username']=='tempusername') {
$fields['username']=$generated_username;
$fields['nickname']=$generated_username;
$fields['display_name']=$generated_username;
$fields['user_nicename']=$generated_username;
}
return $fields;
}// end create_username_from_email
endif;
add_filter('wpmem_register_form','wpmem_hide_username_registration_field');
if ( ! function_exists( 'wpmem_hide_username_registration_field' ) ):
/**
* Remove username field from registration form, then create a hidden field
* with name='username' and set value to 'tempusername' so the form validates
*
*/
function wpmem_hide_username_registration_field($form){
// include PHP DOM parser from http://simplehtmldom.sourceforge.net/
include ('simple_html_dom.php');
$html = str_get_html($form);
// remove username label
$usernamelabel=$html->find('label[for=username]', 0);
// remove username input field and wrapping div
//$usernamediv=$html->find('input[name=log]', 0)->parent ()->outertext='';
$usernamelabel->next_sibling()->outertext='';
$usernamelabel->outertext='';
// append hidden field to the <legend> tag (the beginning of the form)
$legend=$html->find('legend',0);
$legend->outertext=$legend->outertext.'<input type="hidden" name="log" value="tempusername">';
$rows=$html->find('div.div_text, div.div_checkbox');
foreach ($rows as $row)
$row->outertext=$row->outertext.'<br style="clear:both" />';
// these changes are optional, removing 'reset form' button and adding Twitter BS3 classes to the submit button
$clear_button=$html->find('input[name=reset]',0)->outertext='';
$submit_button=$html->find('input[name=submit]',0)->class='btn btn-primary';
return $html;
}
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment