With the help of something @TurtleTread said, I managed to get it working. My code was fine except had to change two things. I removed the custom field from my $user_data, and added this after right after wp_insert_user —
add_user_meta($user_id, 'broker_email', $b_email);
Here’s my final code in my functions.php file —
add_action('template_redirect', 'register_a_user');
function register_a_user(){
if(isset($_GET['do']) && $_GET['do'] == 'register'):
$errors = array();
if(empty($_POST['user']) || empty($_POST['email'])) $errors[] = 'provide a user and email';
if(!empty($_POST['spam'])) $errors[] = 'gtfo spammer';
if(!empty($_POST['pass1']) && !empty($_POST['pass2'])) $error[] = 'The passwords you entered do not match';
$account = esc_attr($_POST['account_type']);
$user_login = esc_attr($_POST['user']);
$user_email = esc_attr($_POST['email']);
$user_pass = esc_attr($_POST['pass1']);
$user_pass2 = esc_attr($_POST['pass2']);
$user_first = $_POST['first_name'];
$user_last = $_POST['last_name'];
$b_email = $_POST['broker_email'];
require_once(ABSPATH.WPINC.'/registration.php');
$sanitized_user_login = sanitize_user($user_login);
$user_email = apply_filters('user_registration_email', $user_email);
if(!is_email($user_email)) $errors[] = 'invalid e-mail';
elseif(email_exists($user_email)) $errors[] = 'this email is already registered';
if(empty($sanitized_user_login) || !validate_username($user_login)) $errors[] = 'invalid user name';
elseif(username_exists($sanitized_user_login)) $errors[] = 'user name already exists';
if(empty($errors)):
if ( $_POST['pass1'] == $_POST['pass2'] ) {
$user_data = array (
'user_login' => $sanitized_user_login,
'user_pass' => $user_pass,
'user_email' => $user_email,
'user_first' => $user_first,
'user_last' => $user_last,
'role' => $account
);
// Create the user
$user_id = wp_insert_user( $user_data );
add_user_meta($user_id, 'broker_email', $b_email);
} else {
$errors[] = 'passwords dont match';
}
if(!$user_id):
$errors[] = 'registration failed...';
else:
wp_new_user_notification($user_id);
endif;
endif;
if(!empty($errors)) define('REGISTRATION_ERROR', serialize($errors));
else define('REGISTERED_A_USER', $user_email);
endif;
}
Also if you want them to be logged in automatically, you can add this function right after —
function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
// You can change home_url() to the specific URL,such as
wp_redirect( 'http://YOURURL.COM' );
}
add_action( 'user_register', 'auto_login_new_user' );