Wordpress自定义注册表单 [英] Wordpress Custom Registration Form

查看:168
本文介绍了Wordpress自定义注册表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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




  • 我需要在此页面上进行自定义设计

  • 我需要添加自定义字段,例如名字,公司,电话等。



有人可以帮助我

.com /> WordPress答案。 Anyhoo,如果你想解决这个没有插件,你需要三件事:


  1. A 自定义WordPress主题

  2. 页面模板

  3. 使用页面模板的 WordPress Page

当您有这三个部分时,您可以在您的页面模板中执行以下操作:

 <?php 
/ *
模板名称:注册
* /

全局$ current_user;
get_currentuserinfo();

$ firstname = $ _POST ['firstname'];
$ lastname = $ _POST ['lastname'];
$ company = $ _POST ['company'];

if(($ firstname!='')&&($ lastname!='')&& TODO:对提交的数据进行更严格的验证

// TODO:生成更好的登录(或询问用户)
$ login = $ firstname。 $ lastname;

// TODO:生成一个更好的密码(或询问用户)
$ password ='123';

// TODO:向用户请求一个电子邮件地址
$ email ='test@example.com';

//创建具有基本必需信息的WordPress用户对象
$ user_id = wp_create_user($ login,$ password,$ email);

if(!$ user_id || is_wp_error($ user_id)){
// TODO:显示错误消息,不要继续。
}

$ userinfo = array(
'ID'=> $ user_id,
'first_name'=> $ firstname,
'last_name '=> $ lastname,
);

//使用名和姓更新WordPress User对象。
wp_update_user($ userinfo);

//将公司添加为用户元数据
update_usermeta($ user_id,'company',$ company);
}

if(is_user_logged_in()):?>

< p>您已登录,无需创建用户个人资料。< / 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>名字:< / label>
< input name =firstname
id =firstname
value =<?php echo esc_attr($ firstname)?&
< / div>
< div class =lastname>
< label for =lastname>姓氏:< / label>
< input name =lastname
id =lastname
value =<?php echo esc_attr($ lastname)?&
< / div>
< div class =company>
< label for =company>公司:< / label>
< input name =company
id =company
value =<?php echo esc_attr($ company)?&
< / div>
< / form>
< / div>

<?php endwhile;万一; ?>

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

  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,你必须照顾之前的代码甚至工作。代码应该也可以分成几个文件,但我希望这是足够让你开始。


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?

解决方案

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

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.

这篇关于Wordpress自定义注册表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆