$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Enqueing External JS on the remote server JS|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Enqueing External JS on the remote server JS

matteradmin9PV0评论
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.

Post a comment

comment list (0)

  1. No comments so far