前面分享了wordpress自定义注册页面的制作,此文为大家分享wordpress自定义登陆页面的制作,感谢露兜的无私分享。
自定义登陆页面和注册页面的方法相同,具体流程如下:
1、复制page.php并重命名为login.php;
2、找到<?php the_content(); ?>标签,在其后添加以下表单代码:
- <?php the_content(); ?>
- <?php if(!emptyempty($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>
- <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; ?>” />
- <input type=“submit” name=“wp-submit” id=“wp-submit” value=“登录” />
- </p>
- </form>
- <?php } else {
- echo ‘<p class=“ludou-error”>您已登录!</p>’;
- } ?>
3、在login.php页面顶部添加以下代码:
- <?php
- /*
- Template Name: 自定义登陆页面
- */
- 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 ( emptyempty($user_name) || ! validate_username( $user_name ) ) {
- $error .= ‘<strong>错误</strong>:请输入有效的用户名。<br />’;
- $user_name = ”;
- }
- if( emptyempty($user_password) ) {
- $error .= ‘<strong>错误</strong>:请输入密码。<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[‘ludou_token’]);
- wp_safe_redirect($redirect_to);
- }
- }
- unset($_SESSION[‘ludou_token’]);
- }
- $rememberme = !emptyempty( $_POST[‘rememberme’] );
- $token = md5(uniqid(rand(), true));
- $_SESSION[‘ludou_token’] = $token;
- ?>
4、后台 》 页面 》 新建页面 》 选择自定义登陆页面模板即可。
声明:本站分享的WordPress主题/插件均遵循 GPLv2 许可协议(免费开源),相关介绍资料仅供学习参考,实际版本可能会因版本迭代或开发者调整而产生变化,如程序中涉及有第三方原创图像、设计模板、远程服务等内容,应获得作者授权后方可使用。本站不提供该程序/软件的产品授权与技术服务,亦不收取相关费用。
评论(0)