0

I have a function that loads a script for validation and i want to add it for only 'login' page. this the function:

function include_regForm_validation() {
        wp_register_script( 'jQuery_form_validation', 'https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.js',array('jquery'), '1.0', true );
        wp_enqueue_script( 'jQuery_form_validation');

    }

I have tried:

if (is_page( 'login' )) {
    add_action( 'wp_enqueue_scripts', 'include_regForm_validation' );
}

but its not working.

3

Put is_page() inside the callback function:

function include_regForm_validation() {
    wp_register_script( 'jQuery_form_validation', 'https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.js',array('jquery'), '1.0', true );

    if ( is_page( 'login' ) ) {
        wp_enqueue_script( 'jQuery_form_validation');
    }
}
add_action( 'wp_enqueue_scripts', 'include_regForm_validation' );

WordPress has not determined what page/archive has been requested yet when hooks are added with add_action(), so you need to make the determination only when wp_enqueue_scripts actually runs, which you do by putting the check into your hooked function.

  • Thanks! works like a charm.. by the way is there any chance where I can put the add_action() to the if condition? – Nilabhra Chakraborty Jul 26 '18 at 5:58
  • Not really, for the reason I explained. Why do you need to do it that way? – Jacob Peattie Jul 26 '18 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.