How to Use WordPress to create a custom registry single plug-in and wordpress form
Source: http://www.ido321.com/1031.html
Original article: Creating a Custom WordPress Registration Form Plugin
Create a customized WordPress plug-in registry ticket
Translator: dwqs
To get started, WordPress provides a custom registry form for new users, or when a new user is added to an existing WordPress site. But what if you want to implement a custom registry list without displaying the WordPress dashboard option?
In this article, we will learn how to create a custom registry ticket in WordPress using a combination of tag templates and short code templates.
The default WordPress registry list consists of only two fields: user name and email address.
This only user name and email form field makes registration very simple. First, you enter a user name, and then enter the mailbox, which is used to receive the password. Next, you can log on to the site using the password received by your mailbox, complete your personal information, and change the password to easy to remember.
Just register at the site, rather than letting the user area experience these pressures, why not provide a direct, containing some additional important form fields besides the user name and email address, for example, password, personal URL, personal profile, nickname, and registry form of their names for users?
This is very useful for multi-user websites 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 about section)
This custom form plug-in can be integrated into WordPress by using short code and contact templates.
With the short code template, you can create a formal registration page on your site. You can also use a short code template in another article, so that users can register after reading your article.
If you want to add a registry ticket to a specific location on the sidebar of your website, you can edit only the location of the tag template in the WordPress topic to create the required registry ticket.
Before creating an email, note that the user name, password, and email fields are required.
When we write verification functions, we enforce these rules.
Build plug-ins
As we said, we started coding the plug-in. First, it contains the plug-in header:
<?php/* 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 will create a PHP function that contains the HTML code of the Registry ticket:
function registration_form( $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio ) { echo ' <style> div { margin-bottom:2px; } input{ margin-bottom:4px; } </style> '; echo ' <form action="' . $_SERVER['REQUEST_URI'] . '" method="post"> <div> <label for="username">Username <strong>*</strong></label> <input type="text" name="username" value="' . ( isset( $_POST['username'] ) ? $username : null ) . '"> </div> <div> <label for="password">Password <strong>*</strong></label> <input type="password" name="password" value="' . ( isset( $_POST['password'] ) ? $password : null ) . '"> </div> <div> <label for="email">Email <strong>*</strong></label> <input type="text" name="email" value="' . ( isset( $_POST['email']) ? $email : null ) . '"> </div> <div> <label for="website">Website</label> <input type="text" name="website" value="' . ( isset( $_POST['website']) ? $website : null ) . '"> </div> <div> <label for="firstname">First Name</label> <input type="text" name="fname" value="' . ( isset( $_POST['fname']) ? $first_name : null ) . '"> </div> <div> <label for="website">Last Name</label> <input type="text" name="lname" value="' . ( isset( $_POST['lname']) ? $last_name : null ) . '"> </div> <div> <label for="nickname">Nickname</label> <input type="text" name="nickname" value="' . ( isset( $_POST['nickname']) ? $nickname : null ) . '"> </div> <div> <label for="bio">About / Bio</label> <textarea name="bio">' . ( isset( $_POST['bio']) ? $bio : null ) . '</textarea> </div> <input type="submit" name="submit" value="Register"/> </form> ';}
Note that the registration field is passed to the above function as a variable. In the function, you will see the following code example:
( isset( $_POST['lname'] ) ? $last_name : null )
This ternary operator checks whether the global variable array $ _ POST contains data. If there is data, it saves the filled form field value to enter the next field.
A registry ticket is complete unless you have verified the form data and cleared the form data. Therefore, we need to createForm verification function of registration_validation.
To simplify the "pain" of verification, we can use the WP_Error class in WordPress. Follow me to compile the verification function:
1. Create a function and pass the field value of the registry form as a function parameter.
function registration_validation( $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio ) {
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.
global $reg_errors;$reg_errors = new WP_Error;
3. Remember: the user name, password, and email we are talking about are required. Do not ignore them. To execute this rule, we need to check whether any of them is empty. If it is null, We will append the error information to the WP_Error class instance.
if ( empty( $username ) || empty( $password ) || empty( $email ) ) { $reg_errors->add('field', 'Required form field is missing');}
4. We can also ensure that the number of characters in the user name is not less than 4
if ( 4 > strlen( $username ) ) { $reg_errors->add( 'username_length', 'Username too short. At least 4 characters is required' );}
5. Check whether the user name has been registered
if ( username_exists( $username ) ) $reg_errors->add('user_name', 'Sorry, that username already exists!');
6. Use the validate_username function of WordPress to ensure that the user name is available.
if ( ! validate_username( $username ) ) { $reg_errors->add( 'username_invalid', 'Sorry, the username you entered is not valid' );}
7. Ensure that the number of characters entered by the user is no less than 5
if ( 5 > strlen( $password ) ) { $reg_errors->add( 'password', 'Password length must be greater than 5' ); }
8. Check whether the email is valid.
if ( !is_email( $email ) ) { $reg_errors->add( 'email_invalid', 'Email is not valid' );}9. Check whether the email is registered
if ( !is_email( $email ) ) { $reg_errors->add( 'email_invalid', 'Email is not valid' );}
10. If a user fills in a website field, check whether the field is valid.
if ( ! empty( $website ) ) { if ( ! filter_var( $website, FILTER_VALIDATE_URL ) ) { $reg_errors->add( 'website', 'Website is not a valid URL' ); }}
11. Finally, we loop through errors in the WP_Error instance and display individual errors.
if ( is_wp_error( $reg_errors ) ) { foreach ( $reg_errors->get_error_messages() as $error ) { echo '<div>'; echo '<strong>ERROR</strong>:'; echo $error . '<br/>'; echo '</div>'; } }
In this way, the verification function is complete. Next
The complete_registration () function is used to process user registration. The user registration is completed through the wp_insert_user function, Your data can be saved as a parameter of this function.
function complete_registration() { global $reg_errors, $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio; if ( 1 > count( $reg_errors->get_error_messages() ) ) { $userdata = 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 <a href="' . get_site_url() . '/wp-login.php">login page</a>.'; }}
In the above function, we use $ reg_errors as an instance of WP_Error and form fields as global variables for global scope.
We need to check whether $ reg_errors contains any errors. If there is no error, insert the user registration information to the WordPress database and use the login link to display the registration information.
Then, place all the previously created functions in the global functions.In custom_registration_function ()
function custom_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 when no WP_error is found complete_registration( $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio ); } registration_form( $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio );}
I need to describe global functions.Code in custom_registration_function.
First, I pass the check$ _ POST ['submit '] is empty to determine whether the form is submitted. If yes, I will call
Registration_validation () function to verify the form submitted by the user.
Then, make sure that the form data is valid and name the valid data after the form field with a variable. Finally, call
The complete_registration () function saves the user. I need to call the registration_form () function to display the user registry list.
As I mentioned earlier, I plan to use short code templates to Support Registration of plug-ins. The following code is supported by the short code module:
// Register a new shortcode: [cr_custom_registration]add_shortcode( 'cr_custom_registration', 'custom_registration_shortcode' ); // The callback function that will replace [book]function custom_registration_shortcode() { ob_start(); custom_registration_function(); return ob_get_clean();}
So far, we have completed the plug-in. The following figure shows the appearance of the Registry ticket.
Note that you may not get the same appearance because the CSS style of the WordPress site is different.
Application plug-in
To use this plug-in on the WordPress article page or independent page, you can add the following code:[cr_custom_registration]
You can also add the listed template tags.<?php custom_registration_function(); ?>.
Is a part of the WordPress topic. You can get this plug-in from the additional code in this article.
Summary
In this article, we created a custom registry ticket step by step and added it to the WordPress site. You can add additional fields
Step to expand the registry form, such as the user role, aol im account, but ensure that the data is valid.
If you have any questions or suggestions, you can leave a comment.
Next article:How to add music to a website
How to Use the custom Form plug-in Wordpress Form Manager
Category-6.php page has introduced wp_head and wp_footer do, plug-in need of resource files need these two to load
A wordpress plug-in that allows you to customize comment forms, add a mobile phone number, and remove email and website
Is this a plug-in ?! My wordpress is basically useless with plug-ins. It takes an hour to write such a simple function ......
The more plug-ins, the slower