如何在没有任何插件的情况下创建 WordPress 自定义登录弹出模式

碧海醫心
发布: 2024-12-07 18:39:42
转载
435人浏览过

如何在没有任何插件的情况下创建 wordpress 自定义登录弹出模式

登录第 1 步:
创建 cusom 登录表单短代码:

// custom sign in popup form shortcod
function custom_login_form() {

    // display the login form
    ob_start();

    ?>
    <form method="post" class="formvalidationquery login-form">
        <div class="form-group">
            <label for="">*user name</label>
            <input type="email" id="youremail" name="useremail" placeholder="email address" required />
            <span class="formerror" id="youremailerror"></span>
        </div>
        <div class="form-group">
        <label for="">*password</label>
            <input type="password" name="password" id="loginpassword" placeholder="password" required />
            <span class="formerror" id="loginpassworderror"></span>
        </div>
        <div class="d-flexinline">                    
            <input type="checkbox" id="checkbox" name="checkbox" />
            <span class="checkboxtext" id="logincheckboxtex">lorem ipsum is simply dummy text of the printing and typesetting industry. 
                lorem ipsum has been the industry's standard dummy text ever since the 1500s</span>
        </div>
        <button type="submit" name="login" class="btn" id="login-btn">
            <span>sign in</span>
            <span class="spinner" style="display: none;"></span>
        </button>                   

    </form>
    <?php
}
add_shortcode('custom_login', 'custom_login_form');
登录后复制

登录第2步:
创建登录表单句柄函数:

// custom sign in poup form handle
function handle_custom_login() {
    if (isset($_post['login'])) {
        $useremail = sanitize_user($_post['useremail']);
        $password = sanitize_text_field($_post['password']);
        $creds = array(
            'user_login'    => $useremail,
            'user_password' => $password,
            'remember'      => isset($_post['remember']),
        );

        $user = wp_signon($creds, false);

        if (is_wp_error($user)) {
            echo '<script>alert("login failed: ' . $user->get_error_message() . '");</script>';
        } else {
            wp_redirect(home_url());
            exit;
        }
    }
}
add_action('init', 'handle_custom_login');
登录后复制

登录第 3 步:
在弹出模式中添加简码。

自定义注册

Uni-CourseHelper
Uni-CourseHelper

私人AI助教,高效学习工具

Uni-CourseHelper 94
查看详情 Uni-CourseHelper

注册第1步:
为 cusom 注册表单简码创建函数:

// custom registration form

function custom_registration_form() {

    ?>
    <form method="post" class="formvalidationqueryregister login-form">
        <div class="form-group">
            <label for="">*user name</label>
            <input type="text" id="yourname" name="username" placeholder="username" required />
        </div>
        <div class="form-group">
            <label for="">*user email</label>
            <input type="email" id="youremail" name="email" placeholder="email" required />
        </div>
        <div class="form-group">
        <label for="">*password</label>
            <input type="password" name="password" id="loginpassword" placeholder="password" required />
            <span class="formerror" id="loginpassworderror"></span>
        </div>
        <div class="d-flexinline">                    
            <input type="checkbox" id="checkbox" name="checkbox" />
            <span class="checkboxtext" id="logincheckboxtex">lorem ipsum is simply dummy text of the printing and typesetting industry. 
                lorem ipsum has been the industry's standard dummy text ever since the 1500s</span>
        </div>
        <button type="submit" name="register" class="btn" id="login-btn">
            <span>sign in</span>
            <span class="spinner" style="display: none;"></span>
        </button>                   

    </form>
    <?php
}
add_shortcode('custom_registration', 'custom_registration_form');


登录后复制

注册第2步:
创建处理注册表单请求的函数:

// custom sign up form handle
function handle_custom_signup() {
    if (isset($_POST['register'])) {
        $username = sanitize_user($_POST['username']);
        $email = sanitize_email($_POST['email']);
        $password = sanitize_text_field($_POST['password']);

        // Check if the username and email already exist
        if (username_exists($username)) {
            echo '<script>alert("Username already exists.");</script>';
            return;
        }
        if (email_exists($email)) {
            echo '<script>alert("Email is already registered.");</script>';
            return;
        }

        // Create a new user
        $user_id = wp_create_user($username, $password, $email);

        if (is_wp_error($user_id)) {
            echo '<script>alert("Error: ' . $user_id->get_error_message() . '");</script>';
        } else {
            echo '<script>alert("Registration successful! You can now log in.");</script>';
        }
    }
}
add_action('init', 'handle_custom_signup');
登录后复制

以上就是如何在没有任何插件的情况下创建 WordPress 自定义登录弹出模式的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号