2011-09-29 64 views
6

Tôi có một khách hàng cần một mẫu đăng ký tùy chỉnh.Mẫu đăng ký tùy chỉnh Wordpress

  • tôi cần phải thực hiện một thiết kế tùy chỉnh trên trang này
  • tôi cần phải thêm các trường tùy chỉnh như First Name, Company, Phone, vv

Ai đó có thể giúp tôi với điều này?

Trả lời

11

Một nơi tốt hơn để đặt câu hỏi WordPress có thể là trên WordPress Answers. Anyhoo, nếu bạn muốn giải quyết này mà không cần plug-in, bạn cần ba điều:

  1. Một custom WordPress theme
  2. Một Page Template
  3. Một WordPress Page sử dụng trang Mẫu

Khi bạn có những ba các bộ phận tại chỗ, bạn có thể thực hiện các thao tác sau trong Mẫu trang của mình:

<?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; ?> 

Bây giờ, khi bạn muốn truy xuất nội dung bạn đã lưu trữ, bạn cần phải biết liệu thông tin có nằm trong chính Đối tượng người dùng hoặc trong siêu dữ liệu hay không. Để lấy lại tên đầu tiên và cuối cùng (của một người dùng đăng nhập):

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

để lấy tên công ty (của một người dùng đăng nhập):

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

Đó là ý chính cơ bản của nó . Vẫn còn rất nhiều thứ thiếu ở đây, như xác nhận, thông báo lỗi, xử lý các lỗi xảy ra trong WordPress API, vv Ngoài ra còn có một số TODO quan trọng mà bạn phải chăm sóc trước khi mã sẽ hoạt động. Mã này có lẽ cũng nên được chia thành nhiều tệp, nhưng tôi hy vọng điều này là đủ để giúp bạn bắt đầu.

Các vấn đề liên quan