• Resolved davidgimenez

    (@davidgimenez)


    Hello everyone, today I bring you an important function which is the separate woocommerce registration form by shortcode to add in function.php

    function show_seller_registration_form() {
    if (!is_user_logged_in() && $_SERVER[‘REQUEST_METHOD’] === ‘POST’) {
    // Validation and sanitization of inputs
    $store_name = isset($_POST[‘store_name’]) ? sanitize_text_field($_POST[‘store_name’]) : ”;
    $username = isset($_POST[‘username’]) ? sanitize_text_field($_POST[‘username’]) : ”;
    $password = isset($_POST[‘password’]) ? $_POST[‘password’] : ”;
    $email = isset($_POST[’email’]) ? sanitize_email($_POST[’email’]) : ”;
    $first_name = isset($_POST[‘first_name’]) ? sanitize_text_field($_POST[‘first_name’]) : ”;
    $last_name = isset($_POST[‘last_name’]) ? sanitize_text_field($_POST[‘last_name’]) : ”;
    $paypal = isset($_POST[‘paypal’]) ? sanitize_text_field($_POST[‘paypal’]) : ”;

        // Check roles
        $user = wp_get_current_user();
        if (in_array('vendor', $user->roles)) {
            echo '<div class="message">You are already a registered seller.</div>';
            return;
        }
    
        // Assign the role 'pending_vendor' to the user
        $user_data = array(
            'user_login' => $username,
            'user_pass'  => $password,
            'user_email' => $email,
            'first_name' => $first_name,
            'last_name'  => $last_name,
            'role'       => 'pending_vendor'
        );
        $user_id = wp_insert_user($user_data);
        if (is_wp_error($user_id)) {
            echo 'Error creating user: ' . $user_id->get_error_message();
        } else {
            // Save the store name and PayPal as user metadata
            update_user_meta($user_id, 'pv_shop_name', $store_name);
            update_user_meta($user_id, 'pv_paypal', $paypal);
            echo '<div class="message">Your account has been created successfully! However, it is pending approval by the administrator. Please check your email for more details.</div>';
        }
    } elseif (!is_user_logged_in()) {
        ob_start();
        ?>
        <form id="seller_registration_form" class="custom-registration-form" action="" method="post">
            <label for="store_name" class="custom-label">Store Name:</label>
            <input type="text" id="store_name" name="store_name" class="custom-input" required><br>
    
            <label for="username" class="custom-label">Username:</label>
            <input type="text" id="username" name="username" class="custom-input" required><br>
    
            <label for="password" class="custom-label">Password:</label>
            <input type="password" id="password" name="password" class="custom-input" required><br>
    
            <label for="email" class="custom-label">Email:</label>
            <input type="email" id="email" name="email" class="custom-input" required><br>
    
            <label for="first_name" class="custom-label">First Name:</label>
            <input type="text" id="first_name" name="first_name" class="custom-input" required><br>
    
            <label for="last_name" class="custom-label">Last Name:</label>
            <input type="text" id="last_name" name="last_name" class="custom-input" required><br>
    
            <label for="paypal" class="custom-label">PayPal Account:</label>
            <input type="text" id="paypal" name="paypal" class="custom-input" required><br>
    
            <input type="submit" value="Register" class="custom-button">
        </form>
    
        <style>
            .custom-registration-form {max-width: 400px;margin: 0 auto;background: #f9f9f9;padding: 20px;border-radius: 8px;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);}
            .custom-label {font-size: 16px;color: #333;}
            .custom-input {width: 100%;padding: 10px;margin-bottom: 10px;border: 1px solid #ccc;border-radius: 4px;font-size: 16px;background-color: #fff!important;}
            .custom-button {background-color: #007bff;color: #fff;border: none;border-radius: 4px;cursor: pointer;font-size: 18px;}
            .custom-button:hover {background-color: #0056b3;}
            .custom-error-message {color: #dc3545;font-size: 14px;}
        </style>
        <?php
        $output = ob_get_clean();
        return $output;
    }

    }
    add_shortcode(‘seller_registration’, ‘show_seller_registration_form’);

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Josh Kohlbach

    (@jkohlbach)

    Thanks for the tip @davidgimenez ??

    I’d suggest one extra thing, make sure when using $_POST/$_GET to not only sanitize the data but also use a nonce check.

    Appreciate you stopping by to drop us the code snippet!

    Thread Starter davidgimenez

    (@davidgimenez)

    I just did a test, I don’t use it for you because I saw that it was something requested in your plugin and they didn’t add it, here I give you the code improved with nonce.

    function show_seller_registration_form() {
    if (!is_user_logged_in() && $_SERVER[‘REQUEST_METHOD’] === ‘POST’) {
    if (isset($_POST[‘seller_registration_nonce’]) && wp_verify_nonce($_POST[‘seller_registration_nonce’], ‘seller_registration_nonce’)) {
    // Validation and sanitization of inputs
    $store_name = isset($_POST[‘store_name’]) ? sanitize_text_field($_POST[‘store_name’]) : ”;
    $username = isset($_POST[‘username’]) ? sanitize_text_field($_POST[‘username’]) : ”;
    $password = isset($_POST[‘password’]) ? $_POST[‘password’] : ”;
    $email = isset($_POST[’email’]) ? sanitize_email($_POST[’email’]) : ”;
    $first_name = isset($_POST[‘first_name’]) ? sanitize_text_field($_POST[‘first_name’]) : ”;
    $last_name = isset($_POST[‘last_name’]) ? sanitize_text_field($_POST[‘last_name’]) : ”;
    $paypal = isset($_POST[‘paypal’]) ? sanitize_text_field($_POST[‘paypal’]) : ”;

            // Check roles
            $user = wp_get_current_user();
            if (in_array('vendor', $user->roles)) {
                echo '<div class="message">You are already a registered seller.</div>';
                return;
            }
    
            // Assign the role 'pending_vendor' to the user
            $user_data = array(
                'user_login' => $username,
                'user_pass'  => $password,
                'user_email' => $email,
                'first_name' => $first_name,
                'last_name'  => $last_name,
                'role'       => 'pending_vendor'
            );
            $user_id = wp_insert_user($user_data);
            if (is_wp_error($user_id)) {
                echo 'Error creating user: ' . $user_id->get_error_message();
            } else {
                // Save the store name and PayPal as user metadata
                update_user_meta($user_id, 'pv_shop_name', $store_name);
                update_user_meta($user_id, 'pv_paypal', $paypal);
                echo '<div class="message">Your account has been created successfully! However, it is pending approval by the administrator. Please check your email for more details.</div>';
            }
        } else {
            echo '<div class="message">Invalid nonce. Please try again.</div>';
        }
    } elseif (!is_user_logged_in()) {
        ob_start();
        ?>
        <form id="seller_registration_form" class="custom-registration-form" action="" method="post">
            <?php wp_nonce_field('seller_registration_nonce', 'seller_registration_nonce'); ?>
            <!-- Rest of your form code goes here -->
        </form>
        <?php
        $output = ob_get_clean();
        return $output;
    }

    }
    add_shortcode(‘seller_registration’, ‘show_seller_registration_form’);

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Gift Code Registration Form’ is closed to new replies.