wordpressQQ登陆php代码_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(!empty($error)) {3 echo '
'.$error.'
';4 }5 if (!is_user_logged_in()) { ?>6
" class="ludou-login">7
8 用戶名
9
10
11
12
13 密碼(至少6位)
14
15
16
17
18
19
20 />
21 記住我22
23
24
25
26
27
28 登錄
29
30
31 <?php } else{32 echo '
您已登錄!(登出?)
';33 } ?>View Code
二、添加表單處理代碼
在page-login.php開頭處中,將第一個 <?php 替換成代碼二(注意使用UTF-8編碼保存),代碼二:
1 <?php2 /**3 * Template Name: 前臺登錄4 * 作者:露兜5 * 博客:https://www.ludou.org/6 *7 * 2013年5月6日 :8 * 首個版本9 *10 * 2013年5月21日 :11 * 防止刷新頁面重復提交數(shù)據(jù)12 */
13
14 if(!isset($_SESSION))15 session_start();16
17 if( isset($_POST['ludou_token']) && ($_POST['ludou_token'] == $_SESSION['ludou_token'])) {18 $error = '';19 $secure_cookie = false;20 $user_name = sanitize_user( $_POST['log'] );21 $user_password = $_POST['pwd'];22 if ( empty($user_name) || ! validate_username( $user_name) ) {23 $error .= '錯誤:請輸入有效的用戶名。
';24 $user_name = '';25 }26
27 if( empty($user_password) ) {28 $error .= '錯誤:請輸入密碼。
';29 }30
31 if($error == '') {32 //If the user wants ssl but the session is not ssl, force a secure cookie.
33 if ( !empty($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'] = !empty( $_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['ludou_token']);65 wp_safe_redirect($redirect_to);66 }67 }68
69 unset($_SESSION['ludou_token']);70 }71
72 $rememberme = !empty( $_POST['rememberme'] );73
74 $token = md5(uniqid(rand(), true));75 $_SESSION['ludou_token'] = $token;
View Code
最后進入WordPress管理后臺 – 頁面 – 創(chuàng)建頁面,標題為登錄(可以自己起名),內容填上登錄說明等,右側可以選擇模板,選擇?前臺登錄?即可。該頁面即前臺登錄頁面。
代碼補充說明
1、如果想讓用戶登錄后跳轉到指定頁面,可以在登錄鏈接后面添加名為 r 的get參數(shù),值為跳轉的鏈接地址,如登錄頁面地址為https://www.ludou.org/login,如果你想讓用戶登錄后跳轉到后臺的文章列表頁,可以把登錄地址改成下面的地址再提供給用戶即可:https://www.ludou.org/login?r=https://www.ludou.org/wp-admin/edit.php
2、如果想美化一下登錄表單,可以在主題的style.css中添加以下代碼:
p.ludou-error {
margin: 16px 0;
padding:12px;
background-color: #ffebe8;
border: 1px solid #c00;
font-size:12px;
line-height: 1.4em;
}.ludou-login label {
color: #777;
font-size:14px;
cursor:pointer;
}.ludou-login .input {
margin: 0;
color: #555;
font-size:24px;
padding:3px;
border: 1px solid #e5e5e5;
background: #fbfbfb;
}
參考文章
-- 完 --
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結
以上是生活随笔為你收集整理的wordpressQQ登陆php代码_WordPress实现前台登录功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gitlab 构建tag_Gitlab详
- 下一篇: 大数据翻页_【干货】大数据翻页的难点和技