WordPress supports foreground login

Source: Internet
Author: User
Tags php file


1. Add a logon form

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

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

3. Search: the_content. Similar code can be found. <? Php the_content ();?>, Replace it with code 1 (note that it is saved using UTF-8 encoding)

If you cannot find the_content in the page-login.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 <? Php the_content ();?>

<? Php the_content ();?>
<? Php if (! Empty ($ error )){
Echo '<p class = "mk-error">'. $ error .'';
}
If (! Is_user_logged_in () {?>
<Form name = "loginform" method = "post" action = "<? Php echo $ _ SERVER ["REQUEST_URI"];?> "Class =" mk-login ">
<P>
<Label for = "log"> user name <br/>
<Input type = "text" name = "log" id = "log" class = "input" value = "<? Php if (! Empty ($ user_name) echo $ user_name ;? /> "Size =" 20 "/>
</Label>
</P>
<P>
<Label for = "pwd"> password (at least 6 digits) <br/>
<Input id = "pwd" class = "input" type = "password" size = "25" value = "" name = "pwd"/>
</Label>
</P>
 
<P class = "forgetmenot">
<Label for = "rememberme">
<Input name = "rememberme" type = "checkbox" id = "rememberme" value = "1" <? Php checked ($ rememberme );? />
Remember Me
</Label>
</P>
 
<P class = "submit">
<Input type = "hidden" name = "redirect_to" value = "<? Php if (isset ($ _ GET ['R']) echo $ _ GET ['R'];? /> "/>
<Input type = "hidden" name = "mk_token" value = "<? Php echo $ token ;? /> "/>
<Button class = "button-primary button-large" type = "submit"> Log on </button>
</P>
</Form>
<? Php} else {
Echo '<p class = "mk-error"> you are logged on! (<A href = "'. wp_logout_url ().'" title = "logout"> logout? </A> )';
}?>

2. Add Form processing code

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

<? Php
/**
* Template Name: foreground logon
* Author: MK
* Blog: http://www.111cn.net/
 * 
* May April 6, 2015:
* First version
 * 
* May April 6, 2015:
* Prevent repeated data submission from refreshing the page
*/
 
If (! Isset ($ _ SESSION ))
Session_start ();
 
If (isset ($ _ POST ['MK _ token']) & ($ _ POST ['MK _ token'] = $ _ SESSION ['MK _ token']) {
$ Error = '';
$ Secure_cookie = false;
$ User_name = sanitize_user ($ _ POST ['log']);
$ User_password = $ _ POST ['pwd'];
If (empty ($ user_name) |! Validate_username ($ user_name )){
$ Error. = '<strong> error: enter a valid user name. <Br/> ';
$ User_name = '';
  }
 
If (empty ($ user_password )){
$ Error. = '<strong> error </strong>: enter the password. <Br/> ';
  }
 
If ($ error = ''){
// If the user wants ssl but the session is not ssl, force a secure cookie.
If (! Empty ($ user_name )&&! Force_ssl_admin ()){
If ($ user = get_user_by ('login', $ user_name )){
If (get_user_option ('use _ ssl ', $ user-> ID )){
$ Secure_cookie = true;
Force_ssl_admin (true );
        }
      }
    }
 
If (isset ($ _ GET ['R']) {
$ Redirect_to = $ _ GET ['R'];
// Redirect to https if user wants ssl
If ($ secure_cookie & false! = Strpos ($ redirect_to, 'WP-admin '))
$ Redirect_to = preg_replace ('| ^ http: // |', 'https: // ', $ redirect_to );
    }
Else {
$ Redirect_to = admin_url ();
    }
 
If (! $ Secure_cookie & is_ssl () & force_ssl_login ()&&! Force_ssl_admin () & (0! = Strpos ($ redirect_to, 'https') & (0 = strpos ($ redirect_to, 'http ')))
$ Secure_cookie = false;
 
$ Creds = array ();
$ Creds ['User _ login'] = $ user_name;
$ Creds ['User _ password'] = $ user_password;
$ Creds ['member'] =! Empty ($ _ POST ['memberme']);
$ User = wp_signon ($ creds, $ secure_cookie );
If (is_wp_error ($ user )){
$ Error. = $ user-> get_error_message ();
    }
Else {
Unset ($ _ SESSION ['MK _ token']);
Wp_safe_redirect ($ redirect_to );
    }
  }
 
Unset ($ _ SESSION ['MK _ token']);
}
 
$ Rememberme =! Empty ($ _ POST ['memberme']);
 
$ Token = md5 (uniqid (rand (), true ));
$ _ SESSION ['MK _ token'] = $ token;
 
 
 
Finally, go to the WordPress management background-page-create page, with the title "login" (you can name it yourself), enter the logon instructions in the content, select a template on the right, and select "log on to the foreground. This page is the front-end login page.
 
Code Supplement
 
1. If you want to jump to the specified page after logging on, you can add the get parameter "r" after the logon link. The value is the link to jump. For example, if the logon page address is login? R = http://www.mk.org/wp-admin/edit.php
 
2nd, if you want to optimize your table list, 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-login label {
Color: #777;
Font-size: 14px;
Cursor: pointer;
}
. Mk-login. 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.