Login Widget

Home » Snippets » Login Widget
0

Created with:

Visibility: 

public

Creator: Shanna Middleton

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Plugin Name: WP Turbo Custom Login
 * Description: Custom login page functionality that redirects users to their profile page using their display name.
 * Version: 1.0.0
 * Author: WP Turbo
 * Author URI: https://wpturbo.dev
 * Text Domain: wpturbo
 */

// If this file is called directly, abort.
if (!defined('WPINC')) {
    die;
}

// Start the session for error message handling
function wpturbo_start_session() {
    if (!session_id()) {
        session_start();
    }
}
add_action('init', 'wpturbo_start_session', 1);

// Display custom login errors
function wpturbo_show_error_messages() {
    if ($codes = wpturbo_get_error_codes()) {
        echo '<div class="wpturbo_error_messages">';
        foreach ($codes as $code) {
            $message = wpturbo_get_error_message($code);
            echo '<span class="error"><strong>' . esc_html__('Error', 'wpturbo') . '</strong>: ' . esc_html($message) . '</span><br/>';
        }
        echo '</div>';
    }
}

// Get error codes from the session
function wpturbo_get_error_codes() {
    return isset($_SESSION['wpturbo_login_errors']) ? $_SESSION['wpturbo_login_errors'] : array();
}

// Get the corresponding error message for a code
function wpturbo_get_error_message($error_code) {
    switch ($error_code) {
        case 'empty_username':
            return __('The username field is empty.', 'wpturbo');
        case 'empty_password':
            return __('The password field is empty.', 'wpturbo');
        default:
            return __('An unknown error occurred.', 'wpturbo');
    }
}

// Create the custom login form
function wpturbo_custom_login_form() {
    if (is_user_logged_in()) {
        return '<p>' . esc_html__('You are already logged in.', 'wpturbo') . '</p>';
    }

    ob_start();

    wpturbo_show_error_messages(); ?>

    <form id="wpturbo_login_form" action="" method="post">
        <fieldset>
            <p>
                <label for="wpturbo_user_login"><?php esc_html_e('Username', 'wpturbo'); ?></label>
                <input name="wpturbo_user_login" id="wpturbo_user_login" class="required" type="text"/>
            </p>
            <p>
                <label for="wpturbo_user_pass"><?php esc_html_e('Password', 'wpturbo'); ?></label>
                <input name="wpturbo_user_pass" id="wpturbo_user_pass" class="required" type="password"/>
            </p>
            <p>
                <input type="hidden" name="wpturbo_login_nonce" value="<?php echo wp_create_nonce('wpturbo-login-nonce'); ?>"/>
                <input type="submit" value="<?php esc_attr_e('Login', 'wpturbo'); ?>"/>
            </p>
            <p>
                <a href="<?php echo wp_lostpassword_url(); ?>"><?php esc_html_e('Lost your password?', 'wpturbo'); ?></a>
            </p>
        </fieldset>
    </form>
    <?php
    return ob_get_clean();
}

// Shortcode to insert the custom login form
add_shortcode('wpturbo_custom_login', 'wpturbo_custom_login_form');

// Process the login form submission
function wpturbo_process_login_form() {
    if (isset($_POST['wpturbo_user_login']) && isset($_POST['wpturbo_user_pass']) && isset($_POST['wpturbo_login_nonce']) && wp_verify_nonce($_POST['wpturbo_login_nonce'], 'wpturbo-login-nonce')) {
        $username = sanitize_text_field($_POST['wpturbo_user_login']);
        $password = $_POST['wpturbo_user_pass'];

        $credentials = [
            'user_login'    => $username,
            'user_password' => $password,
            'remember'      => true
        ];

        $user = wp_signon($credentials, is_ssl());

        if (is_wp_error($user)) {
            $_SESSION['wpturbo_login_errors'] = $user->get_error_codes();
            wp_redirect(wp_get_referer());
            exit;
        } else {
            // Fetch the user's display name to create a profile URL slug
            $user_info = get_userdata($user->ID);
            $display_name_slug = sanitize_title($user_info->display_name);
            
            // Redirect the user to their profile page using the display name
            $profile_url = trailingslashit(home_url('profile')) . $display_name_slug;
            wp_redirect(esc_url($profile_url));
            exit;
        }
    }
}
add_action('init', 'wpturbo_process_login_form');
				

Please copy the entire code block above and paste it into your 'wp-plugin.php' file. This code includes the shortcode [wpturbo_custom_login] that you can use to place the login form on any page or post, and it handles the login process with redirection to the user's profile page using their display name.

Register an account to save your snippets or go Pro to get more features.