0

Im currently working on a front end form using some code found here - orginal

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['first_name']) || empty($_POST['last_name'])) $errors[] = 'provide name';
    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 = esc_attr($_POST['first_name']);
    $user_last = esc_attr($_POST['last_name']);
     $b_email = esc_attr($_POST['broker_email']);
wp_update_user( array( 'ID' => $current_user->ID, 'broker_email' => esc_url( $_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,
 'b_email' => $b_email,
        'role' => $account
    );

    // Create the user
    $user_id = wp_insert_user( $user_data );

} 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;
}

On my register form I tried adding these 3 fields

<input type="text" name="broker_email" class="form-control" value="" />
<input type="text" name="first_name" class="form-control" value="" />
<input type="text" name="last_name" class="form-control" value="" />

Everything almost works fine except the name fields arent saving and the "broker email" fields arent saving. What am I doing wrong?

1

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' );
  • You are still tunnelling on the what makes your code "work" on the surface and ignoring rest of my points. Good luck coding with that naive mentality. 1. I see you kept your esc_attr. Put html special characters in your password, like & < ", and see if you can login with your the passwords you created. 2. The reason your fourth condition isn't affecting your code is because you have singular form error[] instead of the plural. 3. Your created user's first name and last name are not stored because they are not the correct keys. – TurtleTread Nov 14 '17 at 0:37
0

I see you are a designer, so you may not have had much experience with coding and database. From the issues in the code, you have a minimal understanding of PHP and WordPress functions. Do not take this as offense as I'm simply pointing out that you may need to read up a bit more on PHP and WordPress docs (a pain but a necessity if you want to code more working PHP and WP scripts).

Three primary issues that's causing the fields not saving :

  1. Your 4th validation check simply checks if both passwords are not empty, which then always will return true if you supplied input for both fields. Your matching check from the later code is correct and you should use that there.

  2. wp_insert_user takes only pre-defined keys specified in the linked doc. Take a look at that and modify your keys.

  3. Your b_email is a custom usermeta field (If you have database access, you can take a look at the wp_users and the columns there are basically the ones that you can directly update using wp_insert_user or wp_update_user). So to store custom fields, you need to use add_user_meta or update_user_meta (Please read carefully on the parameters of these functions as they allow you add multiple same usermeta keys or only just one unique meta_key depending how you set your params.)

The other function usage issues here and also erroneous code logic:

  1. You used wp_update_user before the wp_insert_user. Is the current user trying to create a new user and store the broker_email as a meta field to himself also?
  2. If that's the case, you need to also use the add_user_meta function, and use get_current_user_id() function rather than calling $current_user as this global variable is not directly accessible in your function. You need to invoke it in your function with global $current_user if you want to access it, but using the WP functions is better.
  3. esc_attr and other filter functions shouldn't be used to alter some user fields which need to stay exactly the same as they were inputted by the user or they will just get a modified login username or password which obviously is not desired in most cases. You can run validation methods on them and tell the user that the fields are not acceptable, but you should not just modify them and save them in the db. For text contents like post content that could be fine as that depend on your system etc, but definitely not for user credentials.

Use PHP manual and WordPress codex to look up specific usage of functions. Understanding exactly how they work is needed to do good WP development.

  • Password validation and everything else works fine like I said. Check my link to the original post. The update user part was just me testing, that wasn't suppose to be there. Your comment about the insert user and add user is what helped me figure it out though. – Rich Nov 13 '17 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.