前面文章介紹過(guò)在WordPress中添加前臺(tái)注冊(cè)功能了,下面我們接著來(lái)介紹在注冊(cè)成功之后增加一個(gè)前臺(tái)登錄功能,具體步驟如下.
一、添加登錄表單
1、首先在當(dāng)前主題的目錄下新建一個(gè)php文件,命名為page-login.php,然后將page.php中的所有代碼復(fù)制到page-login.php中.
2、刪除page-login.php開頭的所有注釋,即 /* 與 */ ,以及它們之間的所有內(nèi)容;
3、搜索:the_content,可以查找到類似代碼< ?php the_content(); ?>,將其替換成代碼一(注意使用UTF-8編碼保存).
如果你在page-login.php中找不到the_content,那么你可以查找:get_template_part,可找到類似代碼:< ?php get_template_part( 'content', 'page' ); ?>,將content-page.php中的所有代碼替換這部分代碼即可。再用下面的代碼替換其中的< ?php the_content(); ?>
- < ?php the_content(); ?>
- < ?php if(!emptyempty($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">用戶名<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">密碼(至少6位)<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 ); ?/> />
- 記住我
- </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; ?/>" /> //Vevb.com
- <button class="button button-primary button-large" type="submit">登錄</button>
- </p>
- </form>
- < ?php } else {
- echo '<p class="mk-error">您已登錄?。?lt;a href="'.wp_logout_url().'" title="登出">登出?</a>)';
- } ?>
二、添加表單處理代碼
在page-login.php開頭處中,將第一個(gè) < ?php 替換成代碼二(注意使用UTF-8編碼保存),代碼二:
- <?php
- /**
- * Template Name: 前臺(tái)登錄
- * 作者:MK
- * 博客:http://www.companysz.com/
- *
- * 2015年4月6日 :
- * 首個(gè)版本
- *
- * 2015年4月6日 :
- * 防止刷新頁(yè)面重復(fù)提交數(shù)據(jù)
- */
- 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 ( emptyempty($user_name) || ! validate_username( $user_name ) ) {
- $error .= '<strong>錯(cuò)誤:請(qǐng)輸入有效的用戶名。<br />';
- $user_name = '';
- }
- if( emptyempty($user_password) ) {
- $error .= '<strong>錯(cuò)誤</strong>:請(qǐng)輸入密碼。<br />';
- }
- if($error == '') {
- // If the user wants ssl but the session is not ssl, force a secure cookie.
- if ( !emptyempty($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['remember'] = !emptyempty( $_POST['rememberme'] );
- $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 = !emptyempty( $_POST['rememberme'] );
- $token = md5(uniqid(rand(), true));
- $_SESSION['mk_token'] = $token;
- ?>
最后進(jìn)入WordPress管理后臺(tái) – 頁(yè)面 – 創(chuàng)建頁(yè)面,標(biāo)題為登錄(可以自己起名),內(nèi)容填上登錄說(shuō)明等,右側(cè)可以選擇模板,選擇 前臺(tái)登錄 即可,該頁(yè)面即前臺(tái)登錄頁(yè)面.
代碼補(bǔ)充說(shuō)明
1、如果想讓用戶登錄后跳轉(zhuǎn)到指定頁(yè)面,可以在登錄鏈接后面添加名為 r 的get參數(shù),值為跳轉(zhuǎn)的鏈接地址,如登錄頁(yè)面地址為http://www.mk.org/login,如果你想讓用戶登錄后跳轉(zhuǎn)到后臺(tái)的文章列表頁(yè),可以把登錄地址改成下面的地址再提供給用戶即可:http://www.mk.org/login?r=http://www.mk.org/wp-admin/edit.php
2、如果想美化一下登錄表單,可以在主題的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;
- }
新聞熱點(diǎn)
疑難解答
圖片精選