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
|
1 Answer
Reset to default 1You 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.
custom-js
already registered. It won't get registered for the second time. 2. There may exist some other function calledfunction_script
on your site and it will be called instead of yours... – Krzysiek Dróżdż Commented Mar 16, 2019 at 6:24