Please or Register to create posts and topics.

Login/Registration page

How could I add more fields on the registration screen for the user to fill in?

I wouldn’t want to use that default wordpress screen, would there be a way to do this using a plugin?

newforum.top has reacted to this post.
newforum.top
Цитата Джонатана , 29 августа 2023 г., 15:30

Как я могу добавить больше полей на экране регистрации, чтобы пользователь мог их заполнить?

Я бы не хотел использовать этот экран WordPress по умолчанию, есть ли способ сделать это с помощью плагина?

Also interested in this question

Jhonathan has reacted to this post.
Jhonathan

I added two fields (given name and family name) in functions.php with the following code:

/*
 *  WP Registration form, require first and last name
 *  and set nickname and display_name to 'first last'
 */

//1.  Create additional fields
add_action( 'register_form', 'fp_register_form' );
function fp_register_form() {

  $first_name = ( ! empty( $_POST['first_name'] ) ) ? trim( $_POST['first_name'] ) : '';
  $last_name = ( ! empty( $_POST['last_name'] ) ) ? trim( $_POST['last_name'] ) : '';

  ?>
  <p>
    <label for="first_name"><?php _e( 'Given Name', 'mydomain' ) ?><br />
    <input type="text" name="first_name" id="first_name" class="input" value="<?php echo esc_attr( wp_unslash( $first_name ) ); ?>" size="25" /></label>
  </p>

  <p>
    <label for="last_name"><?php _e( 'Surname', 'mydomain' ) ?><br />
    <input type="text" name="last_name" id="last_name" class="input" value="<?php echo esc_attr( wp_unslash( $last_name ) ); ?>" size="25" /></label>
  </p>

  <?php
}

//2. Add validation. first_name and last_name are required.
add_filter( 'registration_errors', 'fp_registration_errors', 10, 3 );
function fp_registration_errors( $errors, $sanitized_user_login, $user_email ) {

  if ( empty( $_POST['first_name'] ) || ! empty( $_POST['first_name'] ) && trim( $_POST['first_name'] ) == '' ) {
    $errors->add( 'first_name_error', '<strong>Error</strong>: You must enter your given (first) name.' );
  }
  if ( empty( $_POST['last_name'] ) || ! empty( $_POST['last_name'] ) && trim( $_POST['last_name'] ) == '' ) {
    $errors->add( 'last_name_error', '<strong>Error</strong>: You must enter your surname (last name).' );
  }
  return $errors;
}

//3.  Save our extra registration user data. 'user_register' fires after registration.
add_action( 'user_register', 'fp_user_register' );
function fp_user_register( $user_id ) {
    $first = trim( $_POST['first_name'] );
    $last = trim( $_POST['last_name'] );
    $compound = $first . ' ' . $last;
    update_user_meta( $user_id, 'first_name', $first );
    update_user_meta( $user_id, 'last_name', $last );
    update_user_meta( $user_id, 'nickname', $compound );
    wp_update_user( array( 'ID' => $user_id, 'display_name' => $compound ) );
}

It looks like in the attached image.

 

Uploaded files:
  • Screenshot-2023-09-02-at-3.33.44-PM.jpg
Jhonathan has reacted to this post.
Jhonathan

I found a solution

I used this plugin to create the fields I would like to add and changed the url to access the registration page.

How to Add Custom Fields in WordPress User Registration Form? (Easy Tutorial 2022)

in functions.php I added this code.

add_filter(‘register_url’, ‘my_register_url’);
function my_register_url($register_url) {
    return home_url(‘/cadastrar’);
}
Thank you for your help