I’m developing a theme and trying to get wp_enqueue_script to work. The curious thing, is that nothing shows up. It doesn’t do anything. Here’s my setup:
in functions.php I have:
function named_scripts() {
global $named_options;
if( is_admin() ) return;
wp_deregister_script( 'jquery' );
wp_register_script( 'screen', tz_JS . '/screen.js', array( 'jquery' ) );
wp_enqueue_script( 'screen' );
wp_enqueue_script( 'bootstrap', tz_JS . '/bootstrap/bootstrap.js', array( 'jquery' ) );
wp_register_style( 'custom-style', get_template_directory_uri() . '/css/custom-style.css', array(), '20120208', 'all' );
wp_enqueue_style( 'custom-style' );
}
add_action( 'init', 'named_scripts' );
in header.php I call
named_scripts();
And in the HTML, nothing shows up at all.
You should have registered your jquery file after remove the default wordpress jquery. I use this code.. hope it helps..
Gerald is spot on. You’ve deregistered the jQuery that comes with WordPress without registering an alternative version.
Normally the shipped version is removed if you want to load it directly from a CDN. An example would be below;
If you want to deregister it you need to register another version straight away before enqueing other JS dependant on jQuery
Is the constant “tz_JS” is defined correctly? Presuming yes, you should be able to simplify your function like so:
wp_enqueue_scripts
is the proper hook to use for loading front-end scripts (see Codex). You don’t need to checkis_admin()
sinceadmin_enqueue_scripts
is the corresponding hook for loading scripts on the admin side.If you are developing a child theme use
get_stylesheet_directory_uri()
when loading js within your theme directory.