This article explains how to customize the registration form and account form for WP Video Memberships.
The following are simple examples and should be customized to fit your needs. Any code should be added to either your WordPress themes functions.php file, or another file that is included during initialization.
Customizing The Registration Form
The following code will add an additional Twitter Handle text field to end of the Create Account form that gets generated by the [rvs_create_account] shortcode.
function vs_netflix_custom_registration_fields() { ?>
<div class="rvs-form-row">
<label><?php _e('Twitter Handle', 'vimeo-sync-memberships'); ?></label>
<input name="new_user_twitter_handle" type="text" value="<?php if ( ! empty( $_POST['new_user_twitter_handle'] ) ) esc_attr_e( $_POST['new_user_twitter_handle'] ); ?>" required />
</div>
<?php }
add_action('wpvs_add_fields_after_registration_form', 'vs_netflix_custom_registration_fields');
Customizing The Account Form
In order to keep your customers account fields consistent, you will also need to add the Twitter Handle to the account form, which displays on the Account page.
function vs_netflix_custom_account_fields($user_id) { ?>
<div class="rvs-form-row">
<label><?php _e('Twitter Handle', 'vimeo-sync-memberships'); ?></label>
<input name="new_user_twitter_handle" type="text" value="<?php esc_attr_e( get_user_meta( $user_id, 'wpvs_user_twitter_handle', true ) ); ?>" required />
</div>
<?php }
add_action('wpvs_add_fields_after_account_form', 'vs_netflix_custom_account_fields', 10, 1);
You may notice that this function is identical to the first. Therefore, you can optionally use the same callback function for the wpvs_add_fields_after_account_form, like so:
add_action('wpvs_add_fields_after_account_form', 'vs_netflix_custom_registration_fields', 10, 1);
Checking Custom Field Values
It's always a good idea to validate your custom field values.
function vs_netflix_custom_registration_fields_check($form_fields) {
$field_details = array(
'error' => false,
'message' => null
);
if( isset($form_fields['new_user_twitter_handle']) && empty($form_fields['new_user_twitter_handle']) ) {
$field_details['error'] = true;
$field_details['message'] = __('Please provide a Twitter profile URL', 'vs-netflix');
}
return $field_details;
}
add_filter('wpvs_check_custom_registration_form_fields', 'vs_netflix_custom_registration_fields_check', 10, 1);
Be sure to return an array with an error (boolean) and message (string) variable. Setting error to true means there is an error and the form submission will not be successful.
Saving Custom Fields To Users
Once you have added and validated your custom fields, you will also need to save the information to your users account.
function vs_netflix_apply_custom_registration_fields($form_fields, $user_id) {
if( isset($form_fields['new_user_twitter_handle']) && ! empty($form_fields['new_user_twitter_handle']) ) {
$new_user_twitter_handle = esc_attr( $form_fields['new_user_twitter_handle'] );
update_user_meta($user_id, 'wpvs_user_twitter_handle', $new_user_twitter_handle);
}
}
add_filter('wpvs_apply_custom_registration_form_fields', 'vs_netflix_apply_custom_registration_fields', 10, 2);
Displaying Custom Fields In The Admin Dashboard
Of course, as an administrator, you may want to be able to view and edit your custom fields for each user. The following will add your custom fields to the User Editing page:
add_action( 'show_user_profile', 'vs_netflix_extra_user_profile_fields' );
add_action( 'edit_user_profile', 'vs_netflix_extra_user_profile_fields' );
function vs_netflix_extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Custom Fields", "vs-netflix"); ?></h3>
<table class="form-table">
<tr>
<th><label for="address"><?php _e("Twitter Handle"); ?></label></th>
<td>
<input type="text" name="wpvs_user_twitter_handle" value="<?php echo esc_attr( get_user_meta( $user->ID, 'wpvs_user_twitter_handle', true ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Enter a twitter handle."); ?></span>
</td>
</tr>
</table>
<?php }
You will also need to add the following code to save your custom fields for users when changes are made to their account within the admin dashboard.
add_action( 'personal_options_update', 'vs_netflix_update_profile_fields' );
add_action( 'edit_user_profile_update', 'vs_netflix_update_profile_fields' );
function vs_netflix_update_profile_fields( $user_id ) {
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
if ( isset($_POST['wpvs_user_twitter_handle']) ) {
update_user_meta( $user_id, 'wpvs_user_twitter_handle', $_POST['wpvs_user_twitter_handle'] );
}
}
Related Documentation
Comments
0 comments
Article is closed for comments.