php - Enqueing External JS on the remote server JS

admin2025-06-02  3

wp_register_script( 'custom-js', '.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', '.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'); 
wp_register_script( 'custom-js', 'https://s3.amazonaws/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/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'); 
Share Improve this question edited Mar 16, 2019 at 6:17 Richa Sharma asked Mar 16, 2019 at 6:00 Richa SharmaRicha Sharma 497 bronze badges 3
  • Where did you put that code? – Krzysiek Dróżdż Commented Mar 16, 2019 at 6:16
  • Full code updated. – Richa Sharma Commented Mar 16, 2019 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ż Commented Mar 16, 2019 at 6:24
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748801217a313832.html

最新回复(0)