2011-09-29 166 views
6

我有一个需要自定义注册表单的客户端。Wordpress自定义注册表格

  • 我需要做一个定制设计这个页面
  • 我需要添加自定义字段,如名字,公司,电话,等

有人能帮助我吗?

回答

11

更好的地方问WordPress的问题可能是WordPress Answers。安美居,如果你想解决这个无插件,你需要三样东西:

  1. 一个custom WordPress theme
  2. 一个Page Template
  3. 使用的页面模板

当你有这些三A WordPress Page您可以在页面模板中执行以下操作:

<?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 = '[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; ?> 

现在,当您想要检索已存储的内容时,您需要知道信息是在用户对象本身还是在元数据中。要检索的第一个和最后一个名称(登录用户):

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

要检索的公司名称(登录的用户):

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

这是它的基本精神。还有很多东西在这里丢失,例如验证,错误消息输出,在WordPress API中发生错误的处理等等。还有一些重要的TODO,你必须在代码才能工作之前处理。代码应该也可以分成几个文件,但我希望这足以让你开始。