国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

WordPress實現前臺登錄功能

2024-09-07 00:51:52
字體:
來源:轉載
供稿:網友

前面文章介紹過在WordPress中添加前臺注冊功能了,下面我們接著來介紹在注冊成功之后增加一個前臺登錄功能,具體步驟如下.

一、添加登錄表單

1、首先在當前主題的目錄下新建一個php文件,命名為page-login.php,然后將page.php中的所有代碼復制到page-login.php中.

2、刪除page-login.php開頭的所有注釋,即 /* 與 */ ,以及它們之間的所有內容;

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(); ?>

  1. < ?php the_content(); ?> 
  2. < ?php if(!emptyempty($error)) { 
  3. echo '<p class="mk-error">'.$error.''
  4. if (!is_user_logged_in()) { ?> 
  5. <form name="loginform" method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>" class="mk-login"
  6.     <p> 
  7.       <label for="log">用戶名<br /> 
  8.         <input type="text" name="log" id="log" class="input" value="<?php if(!empty($user_name)) echo $user_name; ?/>" size="20" /> 
  9.       </label> 
  10.     </p> 
  11.     <p> 
  12.       <label for="pwd">密碼(至少6位)<br /> 
  13.         <input id="pwd" class="input" type="password" size="25" value="" name="pwd" /> 
  14.       </label> 
  15.     </p> 
  16.  
  17.     <p class="forgetmenot"
  18.       <label for="rememberme"
  19.         <input name="rememberme" type="checkbox" id="rememberme" value="1" <?php checked( $rememberme ); ?/> /> 
  20.         記住我 
  21.       </label> 
  22.     </p> 
  23.  
  24.     <p class="submit"
  25.       <input type="hidden" name="redirect_to" value="<?php if(isset($_GET['r'])) echo $_GET['r']; ?/>" /> 
  26.       <input type="hidden" name="mk_token" value="<?php echo $token; ?/>" />  //Vevb.com 
  27.       <button class="button button-primary button-large" type="submit">登錄</button> 
  28.     </p> 
  29. </form> 
  30. < ?php } else { 
  31. echo '<p class="mk-error">您已登錄!(<a href="'.wp_logout_url().'" title="登出">登出?</a>)'
  32. } ?> 

二、添加表單處理代碼

在page-login.php開頭處中,將第一個 < ?php 替換成代碼二(注意使用UTF-8編碼保存),代碼二:

  1. <?php 
  2. /** 
  3.  * Template Name: 前臺登錄 
  4.  * 作者:MK 
  5.  * 博客:http://www.survivalescaperooms.com/ 
  6.  *   
  7.  *  2015年4月6日 : 
  8.  *  首個版本 
  9.  *   
  10.  *  2015年4月6日 : 
  11.  *  防止刷新頁面重復提交數據 
  12.  */ 
  13.  
  14. if(!isset($_SESSION)) 
  15.   session_start(); 
  16.  
  17. if( isset($_POST['mk_token']) && ($_POST['mk_token'] == $_SESSION['mk_token'])) { 
  18.   $error = ''
  19.   $secure_cookie = false; 
  20.   $user_name = sanitize_user( $_POST['log'] ); 
  21.   $user_password = $_POST['pwd']; 
  22.   if ( emptyempty($user_name) || ! validate_username( $user_name ) ) { 
  23.     $error .= '<strong>錯誤:請輸入有效的用戶名。<br />'
  24.     $user_name = ''
  25.   } 
  26.  
  27.   ifemptyempty($user_password) ) { 
  28.     $error .= '<strong>錯誤</strong>:請輸入密碼。<br />'
  29.   } 
  30.  
  31.   if($error == '') { 
  32.     // If the user wants ssl but the session is not ssl, force a secure cookie. 
  33.     if ( !emptyempty($user_name) && !force_ssl_admin() ) { 
  34.       if ( $user = get_user_by('login'$user_name) ) { 
  35.         if ( get_user_option('use_ssl'$user->ID) ) { 
  36.           $secure_cookie = true; 
  37.           force_ssl_admin(true); 
  38.         } 
  39.       } 
  40.     } 
  41.  
  42.     if ( isset( $_GET['r'] ) ) { 
  43.       $redirect_to = $_GET['r']; 
  44.       // Redirect to https if user wants ssl 
  45.       if ( $secure_cookie && false !== strpos($redirect_to'wp-admin') ) 
  46.         $redirect_to = preg_replace('|^http://|''https://'$redirect_to); 
  47.     } 
  48.     else { 
  49.       $redirect_to = admin_url(); 
  50.     } 
  51.  
  52.     if ( !$secure_cookie && is_ssl() && force_ssl_login() && !force_ssl_admin() && ( 0 !== strpos($redirect_to'https') ) && ( 0 === strpos($redirect_to'http') ) ) 
  53.       $secure_cookie = false; 
  54.  
  55.     $creds = array(); 
  56.     $creds['user_login'] = $user_name
  57.     $creds['user_password'] = $user_password
  58.     $creds['remember'] = !emptyempty$_POST['rememberme'] ); 
  59.     $user = wp_signon( $creds$secure_cookie ); 
  60.     if ( is_wp_error($user) ) { 
  61.       $error .= $user->get_error_message(); 
  62.     } 
  63.     else { 
  64.       unset($_SESSION['mk_token']); 
  65.       wp_safe_redirect($redirect_to); 
  66.     } 
  67.   } 
  68.  
  69.   unset($_SESSION['mk_token']); 
  70.  
  71. $rememberme = !emptyempty$_POST['rememberme'] ); 
  72.  
  73. $token = md5(uniqid(rand(), true)); 
  74. $_SESSION['mk_token'] = $token
  75. ?> 

最后進入WordPress管理后臺 – 頁面 – 創建頁面,標題為登錄(可以自己起名),內容填上登錄說明等,右側可以選擇模板,選擇 前臺登錄 即可,該頁面即前臺登錄頁面.

代碼補充說明

1、如果想讓用戶登錄后跳轉到指定頁面,可以在登錄鏈接后面添加名為 r 的get參數,值為跳轉的鏈接地址,如登錄頁面地址為http://www.mk.org/login,如果你想讓用戶登錄后跳轉到后臺的文章列表頁,可以把登錄地址改成下面的地址再提供給用戶即可:http://www.mk.org/login?r=http://www.mk.org/wp-admin/edit.php

2、如果想美化一下登錄表單,可以在主題的style.css中添加以下代碼:

  1. <pre lang="php" line="1"
  2. p.mk-error { 
  3.   margin16px 0
  4.   padding12px
  5.   background-color#ffebe8
  6.   border1px solid #c00
  7.   font-size12px
  8.   line-height1.4em
  9. .mk-login label { 
  10.   color#777
  11.   font-size14px
  12.   cursorpointer
  13. .mk-login .input { 
  14.   margin0
  15.   color#555
  16.   font-size24px
  17.   padding3px
  18.   border1px solid #e5e5e5
  19.   background#fbfbfb
  20. }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 惠州市| 井研县| 韶关市| 子长县| 包头市| 留坝县| 牟定县| 临沧市| 花垣县| 丰县| 高台县| 芦山县| 锡林浩特市| 常熟市| 洪洞县| 龙山县| 万全县| 洪雅县| 通州区| 岳西县| 理塘县| 永福县| 香港 | 和政县| 句容市| 拉孜县| 监利县| 陆丰市| 分宜县| 嵊泗县| 鄂托克旗| 平阳县| 多伦县| 昭平县| 永寿县| 桐庐县| 历史| 南丹县| 保亭| 县级市| 宣恩县|