0

I am trying to use the below function to enquue 2 scripts (one internal, one external). The internal one ('custom_script') is working, but the 2nd one ('formValidation') isn't (I get a 'validate() is not a function' error). The validation one is an external link to the script.

Am I doing it wrong? If so, what's the right way to do this?

    function smartdeploy_scripts() {
    // register your script location, dependencies and version
    wp_register_script('custom_script', get_template_directory_uri() . '/CustomJs/SmartDeployCustom.js', array('jquery'), '1.0' );
    wp_register_script('formValidation', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js', array('jquery'), '1.9');

    // enqueue the script
    wp_enqueue_script('custom_script');
    wp_enqueue_script('formValidation');
}
add_action('wp_enqueue_scripts', 'smartdeploy_scripts');

Edit: I've tried qneueing it directly in the plugin file as well, like so. The first enqueue is working ('sdForms'), but the validation on still isn't working.

//Enqueue our js file
function sd_form_scripts() {
    wp_register_script( 'sdForms',  plugin_dir_url( __FILE__ ) . 'js/sdForms.js' );
    wp_enqueue_script( 'sdForms' );

    //wp_register_script( 'validate',  plugin_dir_url( __FILE__ ) . 'js/jquery.validate.js', array('jquery') );
    //wp_enqueue_script( 'validate' );
}
add_action( 'wp_enqueue_scripts', 'sd_form_scripts' );

//Enqueue Validation plugin
function sd_validation_script() {
    wp_register_script('formValidation', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js', array('jquery'));
    wp_enqueue_script('formValidation');
}
add_action('wp_enqueue_scripts', 'sd_validation_script');
3
  • Is that a JavaScript error? Sounds like you are loading the script fine but there's an issue with the JavaScript unless I've misunderstood. If that's so you might get a better answer over on stack overflow. Jul 26, 2016 at 19:41
  • Do you call the plugin from anywhere in your page? Jul 26, 2016 at 19:44
  • I have a small custom plugin I've made that generates some html forms. I am trying to use the validate() function inside that function's main file to validate the forms. I've tried enqueuing it in functions.php as well as directly in the plugin's main php file.
    – Eckstein
    Jul 26, 2016 at 19:52

1 Answer 1

1

It might be the case that you are using you JS validation before enqueuing this script.

You code may be like..

<html>
    $("#myForm").validate(); or JS file that uses validate()
    jquery.validate.js here
</html>

Above code is just for demonstrating.

So you are using validate() function before it has been enqueued so it is throwing validate() is not a function.

Make sure you are using your validate() function after your jquery.validate.js has been enqueued.

Updated Answer

As per your update made, here you are using wp_enqueue_scripts twice which is not needed.

What I understand till now is your sdForms.js contains your validate() method, but still you are loading your jquery.validate.js after sdForms.js. So the updated code will be :

//Enqueue Validation plugin
function sd_validation_script() {
    wp_enqueue_script( 'jqeury' ); // add jqeury
    wp_register_script('formValidation', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js', array('jquery'));
    wp_enqueue_script('formValidation'); // add formValidation plugin

    wp_register_script( 'sdForms',  plugin_dir_url( __FILE__ ) . 'js/sdForms.js' );
    wp_enqueue_script( 'sdForms' ); // add sdForms after formValidation
}
add_action('wp_enqueue_scripts', 'sd_validation_script');
7
  • Hmm, that could be it. I'm enqueing it in the functions file and it is called inside a plugin. Could that be the issue? How would I correct that, if so?
    – Eckstein
    Jul 26, 2016 at 19:46
  • I didnot get you. What is called inside plugin ?
    – Aftab
    Jul 26, 2016 at 19:47
  • I'm trying to use the validate() function within a wordpress plugin's main php file. The Validate script is enqueued within functions.php.
    – Eckstein
    Jul 26, 2016 at 19:48
  • If you are using validate() function in your plugin then please try to add the js file from plugin only. Also how you are calling validate() function in plugin, by enqueuing a different JS in plugin or by using "wp_head" hook ?
    – Aftab
    Jul 26, 2016 at 19:56
  • See my edited answer, I tried it directly in the plugin file as well, with no luck.
    – Eckstein
    Jul 26, 2016 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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