Home >Backend Development >PHP Tutorial >How to Create a WordPress Custom Login Popup Modal without any plugin.

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

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-09 16:26:22728browse

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

sign in Step-1:
Create Cusom login form shortcode:

// 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');

sign in Step-3:
add shortcod in your popup modal.

Custom Sign up

sign up Step-1:
Create function for Cusom Sign up form shortcode:

// 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');

The above is the detailed content of How to Create a WordPress Custom Login Popup Modal without any plugin.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn