Last active
June 7, 2016 11:28
-
-
Save lukecav/284a3088f689f8a3d287b4c63fd4f3bc to your computer and use it in GitHub Desktop.
Custom Registered FIelds Plugin
This file contains 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 | |
/* | |
Plugin Name: Custom Register Fields | |
Plugin URI: http://www.lcavanagh.bluehoststaff.com/ | |
Description: Add first name, last name and job title. | |
Author: Luke Cavanagh | |
Version: 1.0 | |
Author URI: http://www.lcavanagh.bluehoststaff.com/ | |
GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/> | |
*/ | |
//1. Add a new form element... | |
add_action( 'register_form', 'custom_register_form' ); | |
function custom_register_form() { | |
$first_name = ( ! empty( $_POST['first_name'] ) ) ? trim( $_POST['first_name'] ) : ''; | |
$last_name = ( ! empty( $_POST['last_name'] ) ) ? trim( $_POST['last_name'] ) : ''; | |
$job_title = ( ! empty( $_POST['job_title'] ) ) ? trim( $_POST['job_title'] ) : ''; | |
?> | |
<p> | |
<label for="first_name"><?php _e( 'First Name', 'mydomain' ) ?><br /> | |
<input type="text" name="first_name" id="first_name" class="input" value="<?php echo esc_attr( wp_unslash( $first_name ) ); ?>" size="25" /></label> | |
</p> | |
<p> | |
<label for="last_name"><?php _e( 'Last Name', 'mydomain' ) ?><br /> | |
<input type="text" name="last_name" id="last_name" class="input" value="<?php echo esc_attr( wp_unslash( $last_name ) ); ?>" size="25" /></label> | |
</p> | |
<p> | |
<label for="job_title"><?php _e( 'Job Title', 'mydomain' ) ?><br /> | |
<input type="text" name="job_title" id="job_title" class="input" value="<?php echo esc_attr( wp_unslash( $job_title ) ); ?>" size="25" /></label> | |
</p> | |
<?php | |
} | |
//2. Add validation. In this case, we make sure first_name is required. | |
add_filter( 'registration_errors', 'custom_registration_errors', 10, 3 ); | |
function custom_registration_errors( $errors, $sanitized_user_login, $user_email ) { | |
if ( empty( $_POST['first_name'] ) || ! empty( $_POST['first_name'] ) && trim( $_POST['first_name'] ) == '' ) { | |
$errors->add( 'first_name_error', __( '<strong>ERROR</strong>: You must include a first name.', 'mydomain' ) ); | |
} | |
if ( empty( $_POST['last_name'] ) || ! empty( $_POST['last_name'] ) && trim( $_POST['last_name'] ) == '' ) { | |
$errors->add( 'last_name_error', __( '<strong>ERROR</strong>: You must include a last name.', 'mydomain' ) ); | |
} | |
if ( empty( $_POST['job_title'] ) || ! empty( $_POST['job_title'] ) && trim( $_POST['job_title'] ) == '' ) { | |
$errors->add( 'job_title_error', __( '<strong>ERROR</strong>: You must include a job title.', 'mydomain' ) ); | |
} | |
return $errors; | |
} | |
//3. Finally, save our extra registration user meta. | |
add_action( 'user_register', 'custom_user_register' ); | |
function custom_user_register( $user_id ) { | |
if ( ! empty( $_POST['first_name'] ) ) { | |
update_user_meta( $user_id, 'first_name', trim( $_POST['first_name'] ) ); | |
} | |
if ( ! empty( $_POST['last_name'] ) ) { | |
update_user_meta( $user_id, 'last_name', trim( $_POST['last_name'] ) ); | |
} | |
if ( ! empty( $_POST['job_title'] ) ) { | |
update_user_meta( $user_id, 'job_title', trim( $_POST['job_title'] ) ); | |
} | |
} | |
add_action( 'show_user_profile', 'custom_show_extra_profile_fields' ); | |
add_action( 'edit_user_profile', 'custom_show_extra_profile_fields' ); | |
function custom_show_extra_profile_fields( $user ) { ?> | |
<h3>Extra profile information</h3> | |
<table class="form-table"> | |
<tr> | |
<th><label for="job_title">Job Title</label></th> | |
<td> | |
<input type="text" name="job_title" id="job_title" value="<?php echo esc_attr( get_the_author_meta( 'job_title', $user->ID ) ); ?>" class="regular-text" /><br /> | |
<span class="description">Please enter your Job Title.</span> | |
</td> | |
</tr> | |
</table> | |
<?php } | |
add_action( 'personal_options_update', 'custom_save_extra_profile_fields' ); | |
add_action( 'edit_user_profile_update', 'custom_save_extra_profile_fields' ); | |
function custom_save_extra_profile_fields( $user_id ) { | |
if ( !current_user_can( 'edit_user', $user_id ) ) | |
return false; | |
/* Copy and paste this line for additional fields. Make sure to change 'job_title' to the field ID. */ | |
update_usermeta( absint( $user_id ), 'job_title', wp_kses_post( $_POST['job_title'] ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment