Date: 2024-04-03 || Views: 72

Custom Login with WordPress Katorymnd Reaction Plugin

In the realm of WordPress, enhancing user engagement goes beyond mere content presentation. It involves creating an interactive ecosystem where users feel a part of your digital space. The Katorymnd Reaction Plugin steps in as a pioneer, offering more than just a way for users to react to content. It opens up a realm of possibilities for managing user data, especially in customizing the login process to align with your unique user data management strategy.

Integrating Custom User Data

The plugin allows for the specification of a custom database table for user details, deviating from the default WordPress users table. This functionality is crucial for websites with user information stored in custom formats. By specifying your custom table name and mapping column names to expected fields, you enable the Katorymnd Reaction Plugin to seamlessly interact with your user data.

// Specify custom table name

$table_name = 'custom_user_table'; // table storing your custom users details



// Map your custom table columns

$user_id_column_name = 'user_id';

$username_column_name = 'username';

$email_column_name = 'email';

$full_names_column_name = 'full_names'; // full names (both names)

$avatar_column_name = 'avatar'; // profile picture  url link

Customizing the Login Process

Customizing the login process involves capturing user login attempts, validating credentials against your custom user data table, and managing user sessions securely. Here's how you can achieve this with the Katorymnd Reaction Plugin:

/**

 * sample custom login sample

 *

 * Yours may may differ

 * */

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['login_nonce'], $_POST['log'], $_POST['pwd'])) {

    if (wp_verify_nonce($_POST['login_nonce'], 'login_action')) {

        global $wpdb;

        $username = sanitize_text_field($_POST['log']); // Sanitizing the username/email recieved input value

        $password = $_POST['pwd'];

        $table_name = $wpdb->prefix . 'custom_user_table'; // your custom users table



        $user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE email = %s OR username = %s", $username, $username));



        if (!$user) {

            $error_message = 'User not found.';

        } elseif (!wp_check_password($password, $user->password)) {

            $error_message = 'Incorrect password.';

        } else {

            /**

             * katorymnd reaction plugin will use this section in verifying your logged users

             *  so that they can interact with plugin.

             */

            // Generate a secure token and store it in the 'custom_user_sessions' table

            $token = bin2hex(openssl_random_pseudo_bytes(16)); // Generate a secure token

            $cookieExpiration = time() + (DAY_IN_SECONDS * 1); // Set the cookie to expire in 1 day

            setcookie('custom_user_session', $token, $cookieExpiration, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);



            $user_id = isset($user->user_id) ? $user->user_id : $user->id; // Make sure to use the correct user ID property from your user object



            // Define the session table

            $session_table = $wpdb->prefix . 'katorymnd_kr_custom_user_sessions';



            // Check if the table name is configured correctly

            if (empty($table_name) || strpos($table_name, ' ') !== false) {

                $error_message = "Table not configured correctly";



                exit;

            }




            // Check if a session already exists for the user

            $existingSession = $wpdb->get_var($wpdb->prepare(

                "SELECT session_token FROM $session_table WHERE user_id = %d",

                $user_id

            ));



            if ($existingSession) {

                // Session exists, update it

                $success = $wpdb->update(

                    $session_table,

                    array(

                        'session_token' => $token,

                        'expires_at' => date('Y-m-d H:i:s', current_time('timestamp') + (DAY_IN_SECONDS * 1)) // Session set to expire in 1 day

                    ),

                    array('user_id' => $user_id), // Where clause to find the existing session

                    array('%s', '%s'), // Value formats

                    array('%d') // Where formats

                );

            } else {

                // No existing session, insert a new one

                $success = $wpdb->insert(

                    $session_table,

                    array(

                        'user_id' => $user_id,

                        'session_token' => $token,

                        'expires_at' => date('Y-m-d H:i:s', current_time('timestamp') + (DAY_IN_SECONDS * 1)) // Session also set to expire in 1 day

                    ),

                    array('%d', '%s', '%s')

                );

            }



            if (!$success) {

                if (!empty($wpdb->last_error)) {

                    $error_message = "Table not configured correctly";

                    error_log("WordPress database error: " . $wpdb->last_error);

                }

                exit;

            }



            // Your Redirection logic here once logged in

            /*

            if (!headers_sent()) {

                header('Location: ' . esc_url(home_url()));

                exit;

            } else {

                echo "<script>window.location.href='" . esc_js(home_url()) . "';</script>";

                exit;

            }

            */

        }

    } else {

        $error_message = 'Security check failed.';

    }



    if (!empty($error_message)) {

        // Make sure to output $error_message appropriately within your HTML

        echo "<p class='error'>" . esc_html($error_message) . "</p>";

    }

}

Handling Sessions and Redirects

Upon successful login validation, generating a secure token and managing user sessions is paramount. The plugin facilitates the creation of custom user sessions, which are stored in a dedicated table, ensuring a smooth and secure user experience.

Redirect users to the home page or a designated URL after successful login, maintaining a fluid user journey throughout your site. - you can use your redirection logic.

In conclusion, the Katorymnd Reaction Plugin extends WordPress functionality, offering innovative solutions for user engagement and data management. By customizing the login process, site owners can ensure a tailored and secure experience for their users, fostering a sense of belonging and community. Embrace the full potential of your WordPress site with Katorymnd Reaction Plugin, where engagement and customization meet to create a dynamic user experience.



Maximize SEO by leveraging trends, events, and news. Learn how...


Discover how AI is revolutionizing web development, from automated coding...


Discover how Katorymnd Freelancer excels in web design in Uganda...


Katorymnd Portfolio

Here is how I helped my clients reach their goals. Click on the portfolio websites.

Fishman's Fresh Fish Delivery

A custom-built PHP website for online fresh tilapia home delivery.

Online interracial local singles dating site.

Drupal website built for online dating.