• Resolved arathra

    (@arathra)


    Pulling my hair out over this one!

    I have a plugin with a number of js functions. If the js is put directly in the php file it works fine.

    However, I took the js functions out and put them in a new file (in the same directory).

    In the php file I have this:

    function casting_call_register_files() {
    	// css file
    	wp_register_style( 'casting_call-style', plugins_url( 'casting_call.css', __FILE__ ) );
    	wp_enqueue_style( 'casting_call-style' );
    	// js file
    	wp_register_script( 'casting_call-script', plugins_url('casting_call.js', __FILE__ ) );
    	wp_enqueue_script( 'casting_call-script' );
    }
    add_action( 'wp_enqueue_scripts', 'casting_call_register_files' );

    The CSS file loads fine, but the JS file is not found and (obviously) doesn’t work.

    I’ve tried all sorts of things but just cannot get the js to work from another file.

    Any help appreciated!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Check the page’s HTML source, locating the script tag in the head section that links to your JS file. Examine the URL carefully, there is something wrong with the URL if you are getting file not found. Check the URL against the path specification in your FTP client. Something is not matching up. It could be something silly, like casting-call instead of casting_call (hyphen vs. underscore). Click the link in the page source view. What happens?

    Thread Starter arathra

    (@arathra)

    Now that you say it, it becomes obvious!!

    I checked the html source (which I hadn’t done before – d’oh!) which showed a very old version of the js (unversioned so it took the WP version number).

    I then updated the version of the js calling it 5 (a greater number than the WP version) and it all works…

    wp_register_script( 'casting_call-script', plugins_url('casting_call.js', __FILE__ ), false, '5.0', false );

    Many thanks!

    Moderator bcworkz

    (@bcworkz)

    Glad to be a help, though I hadn’t imagined the version would be the issue. It’s frequently not a factor, though there are plenty of examples where it is. Nice work!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘wp_enqueue_scripts not working with js’ is closed to new replies.