一、添加登录表单
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 "
".$error."";
}
if (!is_user_logged_in()) { ?>
< ?php } else {
echo "
您已登录!(登出?)";
} ?>
二、添加表单处理代码
在page-login.php开头处中,将第一个 < ?php 替换成代码二(注意使用UTF-8编码保存),代码二:
< ?php
/**
* Template Name: 前台登录
* 作者:MK
* 博客:http://www.111cn.net/
*
* 2015年4月6日 :
* 首个版本
*
* 2015年4月6日 :
* 防止刷新页面重复提交数据
*/
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 .= "错误:请输入有效的用户名。
";
$user_name = "";
}
if( empty($user_password) ) {
$error .= "错误:请输入密码。
";
}
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["mk_token"]);
wp_safe_redirect($redirect_to);
}
}
unset($_SESSION["mk_token"]);
}
$rememberme = !empty( $_POST["rememberme"] );
$token = md5(uniqid(rand(), true));
$_SESSION["mk_token"] = $token;
最后进入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中添加以下代码:
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;
}
http://m.bbyears.com/wangyezhizuo/74097.html
推荐访问:wordpress教程 wordpress大学