Wordpress Registration Form

I have a client who needs a special registration form.

  • I need to make an individual design on this page.
  • I need to add custom fields like First Name, Company, Phone, etc.

Can anybody help me?

+6
source share
2 answers

The best place to ask WordPress questions is probably on WordPress Answers . Anyhoo, if you want to solve this without plugins, you need three things:

When you have these three parts in place, you can do the following in your page template:

<?php /* Template Name: Registration */ global $current_user; get_currentuserinfo(); $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $company = $_POST['company']; if (($firstname != '') && ($lastname != '') && ($company != '')) { // TODO: Do more rigorous validation on the submitted data // TODO: Generate a better login (or ask the user for it) $login = $firstname . $lastname; // TODO: Generate a better password (or ask the user for it) $password = '123'; // TODO: Ask the user for an e-mail address $email = ' test@example.com '; // Create the WordPress User object with the basic required information $user_id = wp_create_user($login, $password, $email); if (!$user_id || is_wp_error($user_id)) { // TODO: Display an error message and don't proceed. } $userinfo = array( 'ID' => $user_id, 'first_name' => $firstname, 'last_name' => $lastname, ); // Update the WordPress User object with first and last name. wp_update_user($userinfo); // Add the company as user metadata update_usermeta($user_id, 'company', $company); } if (is_user_logged_in()) : ?> <p>You're already logged in and have no need to create a user profile.</p> <?php else : while (have_posts()) : the_post(); ?> <div id="page-<?php the_ID(); ?>"> <h2><?php the_title(); ?></h2> <div class="content"> <?php the_content() ?> </div> <form action="<?php echo $_SERVER['REQUEST_URI'] ?>" method="post"> <div class="firstname"> <label for="firstname">First name:</label> <input name="firstname" id="firstname" value="<?php echo esc_attr($firstname) ?>"> </div> <div class="lastname"> <label for="lastname">Last name:</label> <input name="lastname" id="lastname" value="<?php echo esc_attr($lastname) ?>"> </div> <div class="company"> <label for="company">Company:</label> <input name="company" id="company" value="<?php echo esc_attr($company) ?>"> </div> </form> </div> <?php endwhile; endif; ?> 

Now, when you want to get the material that you saved, you need to know whether this information is in the User object or in the metadata. To get the first and last name (registered user):

 global $current_user; $firstname = $current_user->first_name; $lastname = $current_user->last_name; 

To get the name of the company (registered user):

 global $current_user; $company = get_usermeta($current_user->id, 'company'); 

This is the main point. There are still a lot of missing things, such as validation, error message output, handling errors that occur in the WordPress API, etc. There is also an important TODO that you have to take care of before the code works. The code should probably also be split into several files, but I hope this is enough to get you started.

+11
source

The WordPress custom signup form has two main advantages over the standard form. The first is integration with the overall look of the website theme. Standard forms often do not work well with custom themes, and there is always the possibility that custom CSS files will not render well with the form. On the other hand, a custom form can be easily customized to work with custom CSS.

The second and more popular reason for using a custom registration form is the choice of custom fields that are not included in the standard form. A small custom registration form speeds up the process and collects all the necessary data from a neat interface.

 function wordpress_custom_registration_form( $first_name, $last_name, $username, $password, $email) { global $username, $password, $email, $first_name, $last_name; echo ' <form action="' . $_SERVER['REQUEST_URI'] . '" method="post"> First Name : <input type="text" name="fname" value="' . ( isset( $_POST['fname']) ? $first_name : null ) . '"> Last Name: <input type="text" name="lname" value="' . ( isset( $_POST['lname']) ? $last_name : null ) . '"> Username <strong>*</strong> <input type="text" name="username" value="' . ( isset( $_POST['username'] ) ? $username : null ) . '"> Password <strong>*</strong> <input type="password" name="password" value="' . ( isset( $_POST['password'] ) ? $password : null ) . '"> Email: <strong>*</strong> <input type="text" name="email" value="' . ( isset( $_POST['email']) ? $email : null ) . '"> <input type="submit" name="submit" value="Register"/> </form> '; } 

This form can be inserted anywhere using the [wp_registration_form] shortcode. Here is a snippet of code to install a shortcode:

 function wp_custom_shortcode_registration() { ob_start(); wordpress_custom_registration_form_function(); return ob_get_clean(); } 

I hope that by now you have an honest understanding of creating a custom WordPress registration form. If you don’t understand the verification, create custom WordPress registration forms.

0
source

Source: https://habr.com/ru/post/898274/


All Articles