Home > Backend Development > PHP Tutorial > How to use WordPress to create a custom registration form plug-in, wordpress form_PHP tutorial

How to use WordPress to create a custom registration form plug-in, wordpress form_PHP tutorial

WBOY
Release: 2016-07-13 10:17:08
Original
1021 people have browsed it

How to create a custom registration form plugin using WordPress, wordpress form

Source: http://www.ido321.com/1031.html

Original text: Creating a Custom WordPress Registration Form Plugin

Translation: Create a custom WordPress plugin registration form

Translator: dwqs

Straight to the point, WordPress provides a custom registration form for new users, or when adding a new user to an existing WordPress site. But what if you want to implement a custom registration form without the option to display the WordPress dashboard?

In this article, we will learn how to create a custom registration form in WordPress using a combination of tag templates and shortcode templates.

The WordPress default registration form consists of only two fields - username and email.

The only username and email form fields make registration very simple. First, you enter a username and then your email address, which is used to receive your password. Next, you log in to the site using the password you received in your email, complete your profile, and change your password to something simple and easy to remember.

Just register on the site, instead of letting the user area go through these pressures, then why not provide a direct form field that contains some additional important fields, such as password and personal URL, in addition to username and email? , profile, nickname and their name in the registration form for users to use?

This is very useful for multi-user websites like Tuts+.

In this article, we will build a custom form registration plugin using the following form fields:

  • username
  • password
  • email
  • website URL
  • first name
  • last name
  • nickname
  • biography (or an about section)

This custom form plugin can be integrated into WordPress using shortcodes and contact templates.

Using shortcode templates, you can create a formal registration page in your site. You can also use a shortcode template in a published article so that users can register after reading your article.

If you want to add a registration form to a specific location in the sidebar of your website, you can create the required registration form by editing just the location in your WordPress theme where you want the tag template to be placed.

Before creating, it is important to note that the username, password and email fields are required.

When we write the validation function, we will enforce these rules.

Build Plugin

    As said, we start coding the plugin. First, include the plugin’s header:

<span><?php
<span>/*</span>
<span>  Plugin Name: Custom Registration</span>
<span>  Plugin URI: http://code.tutsplus.com</span>
<span>  Description: Updates user rating based on number of posts.</span>
<span>  Version: 1.0</span>
<span>  Author: Agbonghama Collins</span>
<span>  Author URI: http://tech4sky.com</span>
<span> */</span></span>
Copy after login

Next, we create a PHP function that contains the HTML code for the registration form:

<span>function</span><span> registration_form( $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio ) {
    <span>echo</span> <span>'
    <style>
    div {
        margin-bottom:2px;
    }
     
    input{
        margin-bottom:4px;
    }
    </style>
    '</span>;
 
    <span>echo</span> <span>'
    <form action="'</span> . $_SERVER[<span>'REQUEST_URI'</span>] . <span>'" method="post">
    <div>
    <label for="username">Username <strong>*</strong></label>
    <input type="text" name="username" value="'</span> . ( <span>isset</span>( $_POST[<span>'username'</span>] ) ? $username : <span>null</span> ) . <span>'">
    </div>
     
    <div>
    <label for="password">Password <strong>*</strong></label>
    <input type="password" name="password" value="'</span> . ( <span>isset</span>( $_POST[<span>'password'</span>] ) ? $password : <span>null</span> ) . <span>'">
    </div>
     
    <div>
    <label for="email">Email <strong>*</strong></label>
    <input type="text" name="email" value="'</span> . ( <span>isset</span>( $_POST[<span>'email'</span>]) ? $email : <span>null</span> ) . <span>'">
    </div>
     
    <div>
    <label for="website">Website</label>
    <input type="text" name="website" value="'</span> . ( <span>isset</span>( $_POST[<span>'website'</span>]) ? $website : <span>null</span> ) . <span>'">
    </div>
     
    <div>
    <label for="firstname">First Name</label>
    <input type="text" name="fname" value="'</span> . ( <span>isset</span>( $_POST[<span>'fname'</span>]) ? $first_name : <span>null</span> ) . <span>'">
    </div>
     
    <div>
    <label for="website">Last Name</label>
    <input type="text" name="lname" value="'</span> . ( <span>isset</span>( $_POST[<span>'lname'</span>]) ? $last_name : <span>null</span> ) . <span>'">
    </div>
     
    <div>
    <label for="nickname">Nickname</label>
    <input type="text" name="nickname" value="'</span> . ( <span>isset</span>( $_POST[<span>'nickname'</span>]) ? $nickname : <span>null</span> ) . <span>'">
    </div>
     
    <div>
    <label for="bio">About / Bio</label>
    <textarea name="bio">'</span> . ( <span>isset</span>( $_POST[<span>'bio'</span>]) ? $bio : <span>null</span> ) . <span>'</textarea>
    </div>
    <input type="submit" name="submit" value="Register"/>
    </form>
    '</span>;
}</span>
Copy after login

Please note that the registration fields are passed as variables to the function above. Within the function you will see an example of the following code:

<span>( <span>isset</span>( $_POST[<span>'lname'</span>] ) ? $last_name : <span>null</span> )</span>
Copy after login

This ternary operator checks whether the global variable array $_POST contains data. If there is data, save the filled form field value to enter the next field.

A registration form cannot be considered complete unless you validate the form data and clear the form data, otherwise it does not count. Therefore, we are going to create a form validation function called registration_validation<code>registration_validation<span>的表单验证函数。</span>.

<span>为了简化验证的&rdquo;痛苦&rdquo;,我们可以使用WordPress中的 <span>WP_Error</span> 类。跟着我编写验证函数:</span></span>To simplify the "pain" of validation, we can use the </p>WP_Error<p> class in WordPress. Follow me to write the verification function: <span><span>1、创建函数,并将注册表单的字段值作为函数的参数传递进来</span>

<span>function</span><span> registration_validation( $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio )  {</span>
Copy after login
<p>1. Create a function and pass the field values ​​of the registration form as parameters of the function <span>

<span>global</span><span> $reg_errors;
$reg_errors = <span>new</span> WP_Error;</span>
Copy after login

2. Instantiate the WP_Error class and use the instance as a global variable so that we can use it outside the scope of the function.

<🎜> <🎜><🎜> <🎜><🎜>

3、记住:我们说的用户名、密码和电子邮件是必填的,不要忽略了。为了执行这个规则,我们需要检查它们中任何一个是否为空。如果为空,我们就将错误信息追加给WP_Error 类的实例。

<span>if</span><span> ( <span>empty</span>( $username ) || <span>empty</span>( $password ) || <span>empty</span>( $email ) ) {
    $reg_errors->add(<span>'field'</span>, <span>'Required form field is missing'</span>);
}</span>
Copy after login

4、我们也可以确保用户名的字符个数不小于4

<span>if</span><span> ( 4 > strlen( $username ) ) {
    $reg_errors->add( <span>'username_length'</span>, <span>'Username too short. At least 4 characters is required'</span> );
}</span>
Copy after login

5、检查用户名是否被注册了

<span>if</span><span> ( username_exists( $username ) )
    $reg_errors->add(<span>'user_name'</span>, <span>'Sorry, that username already exists!'</span>);</span>
Copy after login

6、利用WordPress的validate_username函数确保用户名是可用的

<span>if</span><span> ( ! validate_username( $username ) ) {
    $reg_errors->add( <span>'username_invalid'</span>, <span>'Sorry, the username you entered is not valid'</span> );
}</span>
Copy after login

7、确保用户输入的密码的字符个数不小于5

<span>if</span><span> ( 5 > strlen( $password ) ) {
        $reg_errors->add( <span>'password'</span>, <span>'Password length must be greater than 5'</span> );
    }</span>
Copy after login

8、检查电子邮件是否有效

<span>if</span><span> ( !is_email( $email ) ) {
    $reg_errors->add( <span>'email_invalid'</span>, <span>'Email is not valid'</span> );
}</span>
Copy after login
Copy after login
9、检查电子邮件是否被注册
<span>if</span><span> ( !is_email( $email ) ) {
    $reg_errors->add( <span>'email_invalid'</span>, <span>'Email is not valid'</span> );
}</span>
Copy after login
Copy after login

10.、如果用户填写了网站字段,需要检查其是否有效
<span>if</span><span> ( ! <span>empty</span>( $website ) ) {
    <span>if</span> ( ! filter_var( $website, FILTER_VALIDATE_URL ) ) {
        $reg_errors->add( <span>'website'</span>, <span>'Website is not a valid URL'</span> );
    }
}</span>
Copy after login

11、最后,我们在WP_Error实例中对错误进行循环,并显示个别的错误
<span>if</span><span> ( is_wp_error( $reg_errors ) ) {
 
    <span>foreach</span> ( $reg_errors->get_error_messages() <span>as</span> $error ) {
     
        <span>echo</span> <span>'<div>'</span>;
        <span>echo</span> <span>'<strong>ERROR</strong>:'</span>;
        <span>echo</span> $error . <span>'<br/>'</span>;
        <span>echo</span> <span>'</div>'</span>;
         
    }
 
}</span>
Copy after login

这样,验证函数就完成了。接下来是complete_registration()<span>函数,用于处理用户注册。用户的注册真正完成是通过wp_insert_user函数,</span> <span>用户的数据作为数据保存后可以作为此函数的参数。</span> <span><br /> </span>
<span>function</span><span> complete_registration() {
    <span>global</span> $reg_errors, $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio;
    <span>if</span> ( 1 > count( $reg_errors->get_error_messages() ) ) {
        $userdata = <span>array</span>(
        <span>'user_login'</span>    =>   $username,
        <span>'user_email'</span>    =>   $email,
        <span>'user_pass'</span>     =>   $password,
        <span>'user_url'</span>      =>   $website,
        <span>'first_name'</span>    =>   $first_name,
        <span>'last_name'</span>     =>   $last_name,
        <span>'nickname'</span>      =>   $nickname,
        <span>'description'</span>   =>   $bio,
        );
        $user = wp_insert_user( $userdata );
        <span>echo</span> <span>'Registration complete. Goto <a href="'</span> . get_site_url() . <span>'/wp-login.php">login page</a>.'</span>;  
    }
}</span>
Copy after login

在上面的函数中,我们将$reg_errors作为WP_Error的实例,并将表单字段作为全局变量以便于可以再全局作用域中使用。

我们需要检查$reg_errors是否包含任何错误,如果没有错误,则将用户注册信息插入到WordPress的数据库并用登陆链接来显示注册完成的信息。

然后,把所有我们之前创建的函数全部放在全局函数custom_registration_function()<span>之中</span>

<span>function</span><span> custom_registration_function() {
    <span>if</span> ( <span>isset</span>($_POST[<span>'submit'</span>] ) ) {
        registration_validation(
        $_POST[<span>'username'</span>],
        $_POST[<span>'password'</span>],
        $_POST[<span>'email'</span>],
        $_POST[<span>'website'</span>],
        $_POST[<span>'fname'</span>],
        $_POST[<span>'lname'</span>],
        $_POST[<span>'nickname'</span>],
        $_POST[<span>'bio'</span>]
        );
         
        <span>// sanitize user form input</span>
        <span>global</span> $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio;
        $username   =   sanitize_user( $_POST[<span>'username'</span>] );
        $password   =   esc_attr( $_POST[<span>'password'</span>] );
        $email      =   sanitize_email( $_POST[<span>'email'</span>] );
        $website    =   esc_url( $_POST[<span>'website'</span>] );
        $first_name =   sanitize_text_field( $_POST[<span>'fname'</span>] );
        $last_name  =   sanitize_text_field( $_POST[<span>'lname'</span>] );
        $nickname   =   sanitize_text_field( $_POST[<span>'nickname'</span>] );
        $bio        =   esc_textarea( $_POST[<span>'bio'</span>] );
 
        <span>// call @function complete_registration to create the user</span>
        <span>// only when no WP_error is found</span>
        complete_registration(
        $username,
        $password,
        $email,
        $website,
        $first_name,
        $last_name,
        $nickname,
        $bio
        );
    }
 
    registration_form(
        $username,
        $password,
        $email,
        $website,
        $first_name,
        $last_name,
        $nickname,
        $bio
        );
}</span>
Copy after login

我需要说明一下全局函数custom_registration_function()<span>中有哪些代码。</span>

<span> 首先,我通过检查<code>$_POST['submit']<span>是否是空来确定表单是否提交。如果提交了,我就调用</span>

<span><code><span>registration_validation()函数来验证用户提交的表单.</span>

<code><span>然后,确保表单数据的有效性并将有效的数据在表单字段域之后用一个变量命名。最后,调用</span>

<code><span>complete_registration()函数保存用户。我需要调用registration_form()函数来显示用户注册表单。</span>

<code><span>我之前提到过,我打算用短代码模板来支持注册插件。下面就是短代码模的支持代码:</span>

<span>// Register a new shortcode: [cr_custom_registration]</span><span>
add_shortcode( <span>'cr_custom_registration'</span>, <span>'custom_registration_shortcode'</span> );
 
<span>// The callback function that will replace [book]</span>
<span>function</span> custom_registration_shortcode() {
    ob_start();
    custom_registration_function();
    <span>return</span> ob_get_clean();
}</span>
Copy after login

 

到这里为止,我们已经完成了插件,下面的一张图片展示了注册表单的外观。

注意,你可能不会得到完全相同的外观,因为WordPress站点的CSS样式不同。

应用插件

        为了在WordPress的文章页或独立页面使用这个插件,可以加入以下代码:[cr_custom_registration]

<span>也可以添加列出的模板标记<code><?php custom_registration_function(); ?>.,这样可以让表单插件成

<span>为WordPress主题的一个部分。你可以从这篇文章的附加代码得到这个插件。</span>

 

<span><strong>总结</strong></span>

<code><span><strong>   </strong>在这篇文章中,我们一步步创建了一个自定义注册表单并添加到WordPress站点。你可以添加额外字段,进一</span> In this article, we create a custom registration form step by step and add it to your WordPress site. You can add additional fields to further

<span>步扩展这个注册表单,例如用户角色,AOL IM 账户,但是确保数据时有效的。</span>Expand this registration form step by step, such as user role, AOL IM account, but make sure the data is valid.

<span>    如果你有任何问题或建议你,可以留下评论。</span>

If you have any questions or suggestions, you can leave a comment.

<span>下一篇:</span> Next article: <span><br></span>How to add music to the website

<span><br> </span>

How to use the custom form plug-in WordPress Form Manager

Does the category-6.php page introduce wp_head and wp_footer? The resource files required by the plug-in need these two to load

A wordpress plug-in that can customize the comment form, add the input of mobile phone number, and remove the email and website filling in

Does this require a plug-in? ! My wordpress basically uses no plug-ins. For such a simple function, it would take me hours to write one... http://www.bkjia.com/PHPjc/894761.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/894761.htmlTechArticle
How to use WordPress to create a custom registration form plug-in, wordpress form source: http://www.ido321.com/ 1031.html Original text: Creating a Custom WordPress Registration Form Plugin Translation...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template