首页 >后端开发 >php教程 >如何在没有任何插件的情况下创建 WordPress 自定义登录弹出模式。

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

Mary-Kate Olsen
Mary-Kate Olsen原创
2024-12-09 16:26:22728浏览

How to Create a WordPress Custom Login Popup Modal without any plugin.

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

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

    // Display the login form
    ob_start();

    ?>
    <form method="post">



<p><strong>sign in Step-2:</strong><br>
Create login form handle function:<br>
</p>

<pre class="brush:php;toolbar:false">
// 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 步:
在弹出模式中添加简码。

自定义注册

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

// custom registration form

function custom_registration_form() {

    ?>
    <form method="post">



<p><strong>sign up Step-2:</strong><br>
Create function for handle Sign up form request:<br>
</p>

<pre class="brush:php;toolbar:false">
// 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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn