How to use WordPress to create a custom registration form plugin, wordpress form _php Tutorial

Source: Internet
Author: User
Tags wordpress database wordpress dashboard

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


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

Original: Creating a Custom WordPress registration Form Plugin

To create a custom WordPress plugin registration Form

Translator: Dwqs

To the point, WordPress provides a custom registration form for use by new users, or when adding a new user to a WordPress site that already exists. But what if you want to implement a custom registration form without displaying the wordpress dashboard option?

In this article, we will learn how to create a custom registration form in WordPress using a consortium of label templates and short code templates.

WordPress Default Registration Form consists of only two fields-user name and mailbox.

This only user name and mailbox form fields make registration very simple. First, you enter a user name, and then enter the mailbox, which is used to receive the password. Next, you use the mailbox received the password to log on to the site, and complete the personal data, the password changes to easy to remember.

Simply registering at the site rather than letting the user area experience these pressures, why not provide a direct, extra-important form field, such as passwords, personal URLs, profiles, nicknames, and their names, in addition to the user name and mailbox, for users to use?

This is useful for multi-user sites like tuts+.

In this article, we will use the following form fields to create a custom form registration plug-in:

    • Username
    • Password
    • Email
    • Website URL
    • First Name
    • Last Name
    • Nickname
    • Biography (or an on section)

This custom form plugin can be integrated into WordPress by using short code and contact templates.

With the short code template, you can create a formal registration page in your site. You can also post an article with a short code template so that users can register after reading your article.

If you want to add a registration form to a specific location on your site's sidebar, you can create the required registration form by editing the location of the WordPress theme where you only want to place the label template.

Before you create it, it is important to note that the user name, password, and e-mail fields are required.

When we write validation functions, we enforce these rules.

Building plugins

As we said, we started coding the plugin. First, include the header of the plug-in:

/ 
  
    *  Plugin name:custom registration  Plugin uri:http://code.tutsplus.com    description:updates user Rating based on number of posts.   version:1.0  author:agbonghama Collins  Author uri:http://tech4sky.com  */

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

function Registration_form ($username, $password, $email, $website, $first _name, $last _name, $nickname, $bio) {    E Cho'        ;     Echo '        ';}

Note that the registration field is passed as a variable to the above function. In the function, you will see an example of the following code:

isset ($_post[' lname 'null )

This ternary operator checks whether the global variable array $_post contains data and, if there is data, saves the filled form field values to enter the next one.

Unless you validate the form data and clear the form data, a registration form can be completed, otherwise it is not counted. So, we're going to create a name forregistration_validation的表单验证函数。

为了简化验证的”痛苦”,我们可以使用WordPress中的 WP_Error 类。跟着我编写验证函数:

1、创建函数,并将注册表单的字段值作为函数的参数传递进来

function registration_validation ($username, $password, $email, $website, $first _name, $last _name, $nickname, $bio)  {

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

Global  new wp_error;

3, remember: We say the user name, password and email is required, do not ignore. To enforce this rule, we need to check if any of them are empty. If it is empty, we append the error message to an instance of the Wp_error class.

if Empty empty empty  ($email)) {    $reg _errors->add (' field 'Required form field is missing ');}

4, we can also ensure that the number of characters of the user name is not less than 4

if (4 > strlen ($username)) {    ' username_length 'username too short. At least 4 characters is required ' );}

5, check whether the user name is registered

if (Username_exists ($username))    $reg _errors->add (' user_name 'Sorry, that username already Exists! ');

6, using the Validate_username function of WordPress to ensure that the user name is available

if (! validate_username ($username)) {    ' username_invalid 'Sorry, the username you entered are not Valid ' );}

7. Ensure that the number of characters entered by the user is not less than 5

if (5 > strlen ($password)) {        ' password 'password length must be greater than 5 ' ); 
   }

8. Check if e-mail is valid

if (!is_email ($email)) {    ' email_invalid 'email is not valid ' );}
9. Check if e-mail is registered
if (!is_email ($email)) {    ' email_invalid 'email is not valid ' );}

10. If the user fills out the site field, you need to check if it is valid
if  Empty($website)) {    if (! Filter_var ($website, Filter_validate_url)) {        ' website 'website is not a V Alid URL ' );}    }

11. Finally, we loop the error in the Wp_error instance and show the individual error
if (Is_wp_error ($reg _errors)) {     foreach as $error) {             echo';        Echo 'ERROR: ';        Echo '
'; Echo ''; } }

this verifies that the function is complete. Next is complete_registration()函数,用于处理用户注册。用户的注册真正完成是通过wp_insert_user函数, 用户的数据作为数据保存后可以作为此函数的参数。
function complete_registration () {    global $reg _errors, $username, $password, $email, $website, $first _name, $ Last_Name, $nickname, $bio;    if (1 > Count ($reg _errors->get_error_messages ())) {        array(        ' user_login '   = = $username,        ' user_email '    = >   $email,        ' user_pass '     =   $password,        ' User_url '   $website,        ' first_name '    = =   $first _name,        ' last_name '     =   $last _name,        ' nickname '      =   $nickname,        ' description '   =   $bio,        );        $user = Wp_insert_user ($userdata);        Echo ' Registration complete. Goto  '/wp-login.php ' >login page. ';      }} 

In the above function, we will $reg_errors as an instance of Wp_error and use form fields as global variables to make them usable in the global scope.

We need to check if the $reg_errors contains any errors, and if there are no errors, insert the user registration information into the WordPress database and use the login link to display the information of the registration completed.

Then, put all the functions we created earlier into the global functioncustom_registration_function()之中

functionCustom_registration_function () {if(isset($_post[' Submit ']) {registration_validation ($_post[' username '], $_post[' Password '], $_post[' Email '], $_post[' website '], $_post[' fname '], $_post[' lname '], $_post[' nickname '], $_post[' bio ']        );//sanitize user Form Input        Global$username, $password, $email, $website, $first _name, $last _name, $nickname, $bio; $username = Sanitize_user ($_post[' username '] ); $password = Esc_attr ($_post[' Password '] ); $email = Sanitize_email ($_post[' Email '] ); $website = Esc_url ($_post[' website '] ); $first _name = Sanitize_text_field ($_post[' fname '] ); $last _name = Sanitize_text_field ($_post[' lname '] ); $nickname = Sanitize_text_field ($_post[' nickname '] ); $bio = Esc_textarea ($_post[' bio '] );//Call @function complete_registration to create the user        //Only if no wp_error is foundComplete_registration ($username, $password, $email, $website, $first _name, $las    T_name, $nickname, $bio); } registration_form ($username, $password, $email, $website, $first _name, $l Ast_name, $nickname, $bio);}

I need to explain the global functioncustom_registration_function()中有哪些代码。

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

registration_validation()函数来验证用户提交的表单.

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

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

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

//Register a new shortcode: [Cr_custom_registration]  ' cr_custom_registration'custom_registration_shortcode'//The callback function That would replace [book]function Custom_registration_shortcode () {    ob_start ();    Custom_registration_function ();    return Ob_get_clean ();}

So far, we've completed the plugin, and one of the images below shows the appearance of the registration form.

Note that you may not get exactly the same look because the WordPress site has different CSS styles.

Application Plugin

in order to use this plugin on WordPress article pages or standalone pages, you can add the following code:[cr_custom_registration]

也可以添加列出的模板标记 .,这样可以让表单插件成

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

总结

在这篇文章中,我们一步步创建了一个自定义注册表单并添加到WordPress站点。你可以添加额外字段,进一

步扩展这个注册表单,例如用户角色,AOL IM 账户,但是确保数据时有效的。

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

下一篇:How to add music to a Web site




Customizing the form plug-in wordpress form manager how to use

category-6.php page has introduced Wp_head and wp_footer, plug-in required resource files need these two to load

A wordpress plugin that can customize the comment form, increase the number of input mobile phone, remove email and fill in the website

This also need plug-in?! My WordPress basically useless plugin, so simple function, spend an hour to write a ...
The more plug-ins the more slowly

http://www.bkjia.com/PHPjc/894761.html www.bkjia.com true http://www.bkjia.com/PHPjc/894761.html techarticle How to use WordPress to create a custom registration form plugin, wordpress form Source: http://www.ido321.com/1031.html Original: Creating a custom WordPress Registration Form Plugin Translation ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.