Redirect A Page To Homepage If A Specific Input Value Is Empty

Share Now

#Localize the script from function.php

wp_enqueue_script('page-redirect', get_template_directory_uri().
    '/scripts/redirect-page.js', array('jquery'), '1.0', true);

wp_localize_script('page-redirect', 'magicalData', array(

    //'nonce' => wp_create_nonce('wp_rest'),
    'siteURL' => get_site_url()

));

# add this code in JavaScript file

jQuery(document).ready(function($) {

    var siteurl = magicalData.siteURL;
    //console.log(siteurl);

    var order_id = document.querySelector('.final-form-order-id input').value;

    if (order_id == '') {
        console.log('No Order ID');
        window.location.href = siteurl;
    } else {
        console.log(order_id);
    }
});
Picture of Nahid Mahamud

Nahid Mahamud

Web Developer | Graphic Designer | WordPress & Woo-commerce Expert