Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Custom Registration Form

I have a client that needs a custom registration form.

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

Someone can help me with this?

like image 501
Ganikkost Avatar asked Sep 29 '11 10:09

Ganikkost


1 Answers

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

  1. A custom WordPress theme
  2. A Page Template
  3. A WordPress Page that uses the Page Template

When you have these three parts in place, you can do the following in your Page Template:

<?php
/*
Template Name: Registration
*/

global $current_user;
wp_get_current_user();

$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 = '[email protected]';

    // 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 retrieve the stuff you've stored, you need to know whether the information is within the User object itself or in metadata. To retrieve the first and last name (of a logged-in user):

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

To retrieve the company name (of a logged-in user):

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

That's the basic gist of it. There's still a lot of stuff missing here, like validation, error message output, the handling of errors occurring within the WordPress API, etc. There's also some important TODO's that you have to take care of before the code will even work. The code should probably also be split into several files, but I hope this is enough to get you started.

like image 197
Asbjørn Ulsberg Avatar answered Oct 19 '22 17:10

Asbjørn Ulsberg