/******************** Add new field in registration form **************************/
//1. Add a new form element...
add_action( 'register_form', 'myplugin_register_form' );
function myplugin_register_form() {
$usertype = ( ! empty( $_POST['usertype'] ) ) ? trim( $_POST['usertype'] ) : '';?>
<p>
<label for="first_name"><?php _e( 'User Type', 'mydomain' ) ?><br />
<input type="radio" name="usertype" id="usertype" value="eventmanager" />Event Manager
<input type="radio" name="usertype" id="usertype" value="subscriber" />Subscriber
</p>
<?php
}
//2. Add validation. In this case, we make sure User type is required.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['usertype'] ) || ! empty( $_POST['usertype'] ) && trim( $_POST['usertype'] ) == '' ) {
$errors->add( 'usertype_error', __( '<strong>ERROR</strong>: You must select User Type.', 'mydomain' ) );
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {
if ( ! empty( $_POST['usertype'] ) ) {
update_user_meta( $user_id, 'usertype', trim( $_POST['usertype'] ) );
}
}
/************* Display extra fields user profile****************/
add_action ( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action ( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields ( $user ){ ?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="twitter">User Type</label></th>
<td>
<input type="text" name="usertype" id="usertype" value="<?php echo esc_attr( get_the_author_meta( 'usertype', $user->ID ) ); ?>" class="regular-text" /><br />
</td>
</tr>
</table>
<?php }
/************* Update user profile fields ****************/
add_action ( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action ( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_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 'twitter' to the field ID. */
update_usermeta( $user_id, 'usertype', $_POST['usertype'] );
}
/**********************************************************/
//1. Add a new form element...
add_action( 'register_form', 'myplugin_register_form' );
function myplugin_register_form() {
$usertype = ( ! empty( $_POST['usertype'] ) ) ? trim( $_POST['usertype'] ) : '';?>
<p>
<label for="first_name"><?php _e( 'User Type', 'mydomain' ) ?><br />
<input type="radio" name="usertype" id="usertype" value="eventmanager" />Event Manager
<input type="radio" name="usertype" id="usertype" value="subscriber" />Subscriber
</p>
<?php
}
//2. Add validation. In this case, we make sure User type is required.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['usertype'] ) || ! empty( $_POST['usertype'] ) && trim( $_POST['usertype'] ) == '' ) {
$errors->add( 'usertype_error', __( '<strong>ERROR</strong>: You must select User Type.', 'mydomain' ) );
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {
if ( ! empty( $_POST['usertype'] ) ) {
update_user_meta( $user_id, 'usertype', trim( $_POST['usertype'] ) );
}
}
/************* Display extra fields user profile****************/
add_action ( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action ( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields ( $user ){ ?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="twitter">User Type</label></th>
<td>
<input type="text" name="usertype" id="usertype" value="<?php echo esc_attr( get_the_author_meta( 'usertype', $user->ID ) ); ?>" class="regular-text" /><br />
</td>
</tr>
</table>
<?php }
/************* Update user profile fields ****************/
add_action ( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action ( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_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 'twitter' to the field ID. */
update_usermeta( $user_id, 'usertype', $_POST['usertype'] );
}
/**********************************************************/
No comments:
Post a Comment