Wordpress 自定义注册表

新手上路,请多包涵

我有一个客户需要自定义注册表。

  • 我需要在此页面上进行自定义设计
  • 我需要添加自定义字段,如名字、公司、电话等。

有人可以帮我吗?

原文由 Ganikkost 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 261
2 个回答

一个更好的询问 WordPress 问题的地方可能是 WordPress Answers 。 Anyhoo,如果你想在没有插件的情况下解决这个问题,你需要三件事:

  1. 自定义 WordPress 主题
  2. 页面模板
  3. 使用页面模板的 WordPress 页面

准备好这三个部分后,您可以在页面模板中执行以下操作:

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

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

 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 是您必须在代码运行之前处理的。代码可能也应该分成几个文件,但我希望这足以让您入门。

原文由 Asbjørn Ulsberg 发布,翻译遵循 CC BY-SA 4.0 许可协议

使用自定义注册表单的一个优点是可以很容易地根据用户的需要修改代码。对于自定义提交表单,您可以使用 Wordpress 中现有的钩子,例如 template_redirect ,然后将该钩子映射到某个函数,该函数将对表单进行后处理,例如验证和将数据提交到站点的数据库.您可以在 此处 参考一篇深入的文章。

 <div class="employee">
<input type="hidden" name="show_msg">

<form name="customer_details" method="POST" required="required" class="input-hidden">
Your Name: <input type="text" id="name" name="customer_name">
Your Email: <input type="text" id="email" name="customer_email">
Company: <input type="text" id="company" name="company">
Sex: <input type="radio" name="customer_sex" value="male">Male <input type="radio" name="customer_sex" value="female">Female
<textarea id="post" name="experience" placeholder="Write something.." style="height:400px;width:100%"></textarea>
<input type="submit" value="Submit">
<!--?php wp_nonce_field( 'wpshout-frontend-post','form-submit' ); ?-->
</form></div>

函数

function wpshout_frontend_post() {
    wpshout_save_post_if_submitted();
}
add_action('template_redirect','wpshout_frontend_post', 2);

原文由 rajive bisht 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题