Wordpress Custom Registration Form

前端 未结 2 1911
半阙折子戏
半阙折子戏 2020-12-31 20:14

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, Compan
2条回答
  •  清酒与你
    2020-12-31 20:23

    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:

     $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()) : ?>
    
      

    You're already logged in and have no need to create a user profile.

    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.

提交回复
热议问题