Skip to content

Instantly share code, notes, and snippets.

@joelstransky
Created February 12, 2016 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelstransky/7685dd4ed415d5d467e3 to your computer and use it in GitHub Desktop.
Save joelstransky/7685dd4ed415d5d467e3 to your computer and use it in GitHub Desktop.
How to customize a Wordpress Multisite (WPMU) registration form
<?php
/**
* Customize the MutltiSite Registration Form
*/
//1. Add a new form element...
add_action( 'signup_extra_fields', 'mydomain_register_form' );
function mydomain_register_form( $errors ) {
// First name
$first_name = ( ! empty( $_POST['first_name'] ) ) ? trim( $_POST['first_name'] ) : '';
echo '<label for="first_name">' . __('First Name:') . '</label>';
if ( $errmsg = $errors->get_error_message('first_name_error') ) {
echo '<p class="error">'.$errmsg.'</p>';
}
echo '<input name="first_name" type="text" id="first_name" value="'. esc_attr($first_name) .'" maxlength="60" /><br />';
_e( '(Must be at least 4 characters, letters and numbers only.)' );
}
//2. Add validation. In this case, we make sure first_name is required.
add_filter( 'wpmu_validate_user_signup', 'mydomain_registration_errors', 10, 3 );
function mydomain_registration_errors( $result ) {
if ( empty( $_POST['first_name'] ) || ! empty( $_POST['first_name'] ) && trim( $_POST['first_name'] ) == '' ) {
$result['errors']->add( 'first_name_error', __( '<strong>ERROR</strong>: You must include a first name.', 'mydomain' ) );
}
return $result;
}
//3. add meta for the pending user.
add_filter( 'add_signup_meta', 'mydomain_signup_meta' );
function mydomain_signup_meta( $meta ) {
if ( ! empty( $_POST['first_name'] ) ) {
$meta['first_name'] = $_POST['first_name'];
}
return $meta;
}
// 4. Finally, Set new usermeta upon new user activation
add_action( 'wpmu_activate_user', 'mydomain_activate_user', 10, 3 );
function mydomain_activate_user( $user_id, $email, $meta ) {
if ( $meta['first_name'] ) {
update_user_meta( $user_id, 'first_name', trim( $meta['first_name'] ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment