0
wp_register_script( 'custom-js', 'https://s3.amazonaws.com/codexo/custom.js', array( 'jquery' ), '1.1', true );
        wp_enqueue_script( 'custom-js' );

Above is the function through which I am trying to enqueue an external javascript, but this is not working. where am I going wrong and how can I fix this?

Full code here:

if ( ! function_exists( 'function_script' ) ) {

function function_script() {
        // Register the script like this for a theme:
        wp_register_script( 'custom-js', 'https://s3.amazonaws.com/codexo/custom.js', array( 'jquery' ), '1.1', true );
        wp_enqueue_script( 'custom-js' );


        wp_enqueue_style( 'styles', THEMEROOT . '/style.css' );
        // wp_enqueue_style( 'style', get_template_directory_uri() . '/style.css' );
        // wp_enqueue_style( 'responsive', THEMEROOT . '/css/responsive.css', array('styles'));
    }
}
add_action('wp_enqueue_scripts','function_script'); 
  • Where did you put that code? – Krzysiek Dróżdż Mar 16 at 6:16
  • Full code updated. – Richa Sharma Mar 16 at 6:19
  • 1
    I don't see any problem in there. Two problems that may occur: 1. There can be script with handle custom-js already registered. It won't get registered for the second time. 2. There may exist some other function called function_script on your site and it will be called instead of yours... – Krzysiek Dróżdż Mar 16 at 6:24
1

You use $ in custom.js file and probably that's the reason, because jQuery is included in WordPress in noConflict mode.

You can replace $ with jQuery in custom.js or wrap the code as shown below:

(function( $ ) {

    // --- your code ---
    $(document).ready(function() {
       //...
    });

})(jQuery);

Or

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

    // code from file whithout line "$(document).ready(function(){"
    //...

});

More about using jQuery in Wordpress you can read in "JavaScript Best Practices" on codex.

  • Yes you was right. This was the deal breaker: jQuery(document).ready(function($) – Richa Sharma Mar 16 at 8:38
  • I will always remember this rest of my life now. – Richa Sharma Mar 16 at 8:41

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.