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(); ?>
代码一
<?php the_content(); ?>
<?php if(!empty($error)) {
echo '<p class="ludou-error">'.$error.'</p>';
}
if (!is_user_logged_in()) { ?>
<form name="loginform" method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>" class="ludou-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="ludou_token" value="<?php echo $token; ?>" />
<button class="button button-primary button-large" type="submit">登录</button>
</p>
</form>
<?php } else {
echo '<p class="ludou-error">您已登录!(<a href="'.wp_logout_url().'" title="登出">登出?</a>)</p>';
} ?>
二、添加表单处理代码
在page-login.php开头处中,将第一个 <?php 替换成代码二(注意使用UTF-8编码保存),代码二:
<?php
/**
* Template Name: 前台登录
* 作者:露兜
* 博客:https://www.ludou.org/
*
* 2013年5月6日 :
* 首个版本
*
* 2013年5月21日 :
* 防止刷新页面重复提交数据
*/ if(!isset($_SESSION))
session_start(); if( isset($_POST['ludou_token']) && ($_POST['ludou_token'] == $_SESSION['ludou_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>错误</strong>:请输入有效的用户名。<br />';
$user_name = '';
} if( empty($user_password) ) {
$error .= '<strong>错误</strong>:请输入密码。<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['remember'] = !empty( $_POST['rememberme'] );
$user = wp_signon( $creds, $secure_cookie );
if ( is_wp_error($user) ) {
$error .= $user->get_error_message();
}
else {
unset($_SESSION['ludou_token']);
wp_safe_redirect($redirect_to);
}
} unset($_SESSION['ludou_token']);
} $rememberme = !empty( $_POST['rememberme'] ); $token = md5(uniqid(rand(), true));
$_SESSION['ludou_token'] = $token;
最后进入WordPress管理后台 – 页面 – 创建页面,标题为登录(可以自己起名),内容填上登录说明等,右侧可以选择模板,选择 前台登录 即可。该页面即前台登录页面。
代码补充说明
1、如果想让用户登录后跳转到指定页面,可以在登录链接后面添加名为 r 的get参数,值为跳转的链接地址,如登录页面地址为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;
}
参考文章
Function Reference/validate username
Function Reference/wp signon
Function Reference/wp safe redirect
-- 完 --
WordPress实现前台登录功能的更多相关文章
- WordPress添加前台注册功能
一.添加注册表单 1.首先在当前主题的目录下新建一个php文件,命名为reg-page.php,然后将page.php中的所有代码复制到reg-page.php中: 2.删除reg-page.php开 ...
- Flask实战第46天:完成前台登录功能
后台逻辑 首先进行表单验证, 编辑front.froms.py ... class SignInForm(BaseForm): telephone = StringField(validators=[ ...
- Spring+Hibernate+Struts2整合之实现登录功能
前端代码: <form id="loginForm" action="${ pageContext.request.contextPath }/user_login ...
- 如何修改隐藏Zblog/WordPress默认后台登录地址
我相信很多博主站长都遇到过站点被暴力破解,虽然未被破解,但是经常收到那些尝试登录失败的邮件提醒也会心慌慌的.对于这种情况,最好的办法就是修改/隐藏我们的后台登录地址. 关于zblogASP后台登录地址 ...
- 一步步开发自己的博客 .NET版(3、注册登录功能)
前言 这次开发的博客主要功能或特点: 第一:可以兼容各终端,特别是手机端. 第二:到时会用到大量html5,炫啊. 第三:导入博客园的精华文章,并做分类.(不要封我) 第四:做 ...
- php之登录功能实现。
项目默认存在的东西:jquery库[jquery.min.js] 登录功能实现的基本逻辑: 1.书写前台php功能基本页面:(index.php) a.编写基本功能,比如用户名.密码.登录 b.引用j ...
- Struts2整合Hibernate3实现用户登录功能
所用技术:struts2 ,hibernate,jsp,mysql 本DEMO仅仅实现用户登录功能,采用MVC思想,自己也觉得相对是比较简单,比较容易理解数据流向的一个例子,通过整合这个过程,能够清晰 ...
- .Net Webapi Swagger增加登录功能
.mytitle { background: #2B6695; color: white; font-family: "微软雅黑", "宋体", "黑 ...
- node+vue进阶【课程学习系统项目实战详细讲解】打通前后端全栈开发(1):创建项目,完成登录功能
第一章 建议学习时间8小时·分两次学习 总项目预计10章 学习方式:详细阅读,并手动实现相关代码(如果没有node和vue基础,请学习前面的vue和node基础博客[共10章]) 视频教程地 ...
随机推荐
- 头部和信号栏一个颜色appcloud
<header id="header" > <ul > <li class="active" onclick="api. ...
- .Net应用导入、导出Excel文件
本次阐述的导入和导出都围绕此Demo进行
- Negut 上传乱码
解决办法 修改 bat 文件的 格式为ANSI格式即可
- CF1066E Binary Numbers AND Sum
思路: 模拟.实现: #include <iostream> using namespace std; ; ], b[]; ]; int main() { int n, m; while ...
- cacti添加被监控机全过程
在被监控端上的操作: 1.在被监控机器上root目录下建立文件 test.sh chmod 777 test.sh cat test #!/bin/bash echo $RANDOM 2.在snmpd ...
- selenium-Python之鼠标事件
通过click()来模拟鼠标的单击操作,鼠标还具有鼠标右击,双击,悬停甚至鼠标拖动等功能.在webdriver中,将这些鼠标操作方法封装在ActionChains类提供. ActionChains类提 ...
- navicate与mysql连接的中文乱码问题
1. 在navicate中查看 show variables like'char%'; show variables like 'collation_%'; 2.在mysql中查看 通过对比可以发现两 ...
- httpmodule初识
.net的请求流程: HttpRequest-->inetinfo.exe->ASPNET_ISAPI.DLL-->Http Pipeline-->ASPNET_WP.EXE- ...
- ucosii(2.89)mutex 应用要点
mutex 的创建在于共享资源打交道是可以可以保证满足互斥条件:1,必须保证继承优先级要高于可能与相应共享资源打交道的任务中优先级最高的优先级.2,不要将占有Mutex的任务挂起,也不要让占有mute ...
- JAVA中IP和整数相互转化(含有掩码的计算)
import java.net.InetAddress;/** * 用于IP和整数之间的相互转换 * @author Andy.Wang * */public class IPv4Util { ...