WordPress implements the frontend registration function

Source: Internet
Author: User
Tags php file


1. Add a registry ticket

1. First create a php file under the Directory of the current topic, name it reg-page.php, and then copy all the code in page. php to the reg-page.php;

2. Delete all comments starting with the reg-page.php, I .e./* and */, and all contents between them;

3. Search for the_content. You can find similar code: <? Php the_content ();?>, Replace this code with code 1 (note that using UTF-8 encoding to save as replacing the original reg-page.php)

If you cannot find the_content in the reg-page.php, you can find: get_template_part, you can find similar code: <? Php get_template_part ('content', 'page');?>, Replace all the code in the content-page.php with this code. Replace it with the following code <? Php the_content ();?>


<? Php the_content ();?>
<? Php if (! Empty ($ error )){
Echo '<p class = "ludou-error">'. $ error .'';
}

<? Php
/**
* Template Name: front-end registration
* Author: MK
* Blog: http://www.111cn.net/
 * 
* May April 05, 2015:
*/
 
If (! Empty ($ _ POST ['MK _ reg ']) {
$ Error = '';
$ Sanitized_user_login = sanitize_user ($ _ POST ['User _ login']);
$ User_email = apply_filters ('User _ registration_email ', $ _ POST ['User _ email']);
 
// Check the username
If ($ sanitized_user_login = ''){
$ Error. = '<strong> error: enter the user name. <Br/> ';
} Elseif (! Validate_username ($ sanitized_user_login )){
$ Error. = '<strong> error </strong>: The user name contains invalid characters. Enter a valid user name <br/>. ';
$ Sanitized_user_login = '';
} Elseif (username_exists ($ sanitized_user_login )){
$ Error. = '<strong> error </strong>: The user name has been registered. Please select another one. <Br/> ';
  }
 
// Check the e-mail address
If ($ user_email = ''){
$ Error. = '<strong> error </strong>: Enter the email address. <Br/> ';
} Elseif (! Is_email ($ user_email )){
$ Error. = '<strong> error </strong>: The email address is incorrect .! <Br/> ';
$ User_email = '';
} Elseif (email_exists ($ user_email )){
$ Error. = '<strong> error </strong>: The email address has been registered. Change it. <Br/> ';
  }
 
// Check the password
If (strlen ($ _ POST ['User _ pass']) <6)
$ Error. = '<strong> error: The password must contain at least six characters! <Br/> ';
Elseif ($ _ POST ['User _ pass']! = $ _ POST ['User _ pass2 '])
$ Error. = '<strong> error </strong>: The two passwords must be consistent! <Br/> ';
 
If ($ error = ''){
$ User_id = wp_create_user ($ sanitized_user_login, $ _ POST ['User _ pass'], $ user_email );
 
If (! $ User_id ){
$ Error. = sprintf ('<strong> error </strong>: your registration request cannot be completed... contact <a href = \ "mailto: % s \"> administrator </a>! <Br/> ', get_option ('admin _ email '));
    }
Else if (! Is_user_logged_in ()){
$ User = get_userdatabylogin ($ sanitized_user_login );
$ User_id = $ user-> ID;
 
// Automatic logon
Wp_set_current_user ($ user_id, $ user_login );
Wp_set_auth_cookie ($ user_id );
Do_action ('WP _ login', $ user_login );
    }
  }
}
If (! Is_user_logged_in () {?>
<Form name = "registerform" method = "post" action = "<? Php echo $ _ SERVER ["REQUEST_URI"];?> "Class =" mk-reg ">
<P>
<Label for = "user_login"> user name <br/>
<Input type = "text" name = "user_login" tabindex = "1" id = "user_login" class = "input" value = "<? Php if (! Empty ($ sanitized_user_login) echo $ sanitized_user_login ;? /> "/>
</Label>
</P>
 
<P>
<Label for = "user_email"> email <br/>
<Input type = "text" name = "user_email" tabindex = "2" id = "user_email" class = "input" value = "<? Php if (! Empty ($ user_email) echo $ user_email ;? /> "Size =" 25 "/>
</Label>
</P>
 
<P>
<Label for = "user_pwd1"> password (at least 6 digits) <br/>
<Input id = "user_pwd1" class = "input" tabindex = "3" type = "password" tabindex = "21" size = "25" value = "" name = "user_pass "/>
</Label>
</P>
 
<P>
<Label for = "user_pwd2"> duplicate password <br/>
<Input id = "user_pwd2" class = "input" tabindex = "4" type = "password" tabindex = "21" size = "25" value = "" name = "user_pass2 "/>
</Label>
</P>
 
<P class = "submit">
<Input type = "hidden" name = "mk_reg" value = "OK"/>
<Button class = "button-primary button-large" type = "submit"> register </button>
</P>
</Form>
<? Php} else {
Echo '<p class = "mk-error"> You have registered and logged on! ';
}?>
2. Add Form processing code

At the beginning of the reg-page.php, place the first <? Php changed to code 2 (note the use of UTF-8 encoding to save ):

Finally, go to the WordPress management background-page-create page, with the title registration (you can name it yourself), and enter the registration instructions in the content. On the right side, select a template and select "register at the front end. This page is the front-end registration page. Place the link on this page to any location of the website for users to click registration.

Code Supplement

1. If the registration is successful, the user will log on automatically. If you do not need the automatic function, you can delete line 49-55 of code and replace it:


// After successful registration, jump to other pages on the site and modify the URL.
Wp_safe_redirect ('http: // www.111cn.net /');
 
 
2nd, if you want to optimize the registration form, you can add the following code to the topic's style.css:
 
<Pre lang = "php" line = "1">
P. mk-error {
Margin: 16px 0;
Padding: 12px;
Background-color: # ffebe8;
Border: 1px solid # c00;
Font-size: 12px;
Line-height: 1.4em;
}
. Mk-reg label {
Color: #777;
Font-size: 14px;
Cursor: pointer;
}
. Mk-reg. input {
Margin: 0;
Color: #555;
Font-size: 24px;
Padding: 3px;
Border: 1px solid # e5e5e5;
Background: # fbfbfb;
}

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.