HTML and CSS Reference
In-Depth Information
'/wp-content/themes/my-theme/custom.js' ,
array ( 'theme-base' ),
'1.0.0' ,
add_action ( 'init' , 'mytheme_register_scripts' );
There is no need to register jQuery, because WordPress already has. Re-registering it could
lead to problems.
All of this registering JavaScript files the WordPress way has, so far, achieved nothing.
Nothing will be outputted to your HTML files.
To get WordPress to output the relevant HTML, we need to enqueue our files. Unlike the
relatively long-winded commands required to register the functions, this is a very simple
Outputting the JavaScript HTML
Adding the <script> tags to your HTML is done with the wp_enqueue_script function. Once
a script is registered, it takes one argument, the file's handle.
Adding JavaScript to the HTML is done in the wp_print_scripts hook with the following code:
function mytheme_enqueue_scripts (){
if ( ! is_admin ()):
wp_enqueue_script ( 'theme-custom' ); //custom.js
endif ; //!is_admin
add_action ( 'wp_print_scripts' , 'mytheme_enqueue_scripts' );
Of our two registered JavaScript files ( base.js and custom.js ), only the second adds
JavaScript functionality to the website. Without the second file, there is no need to add the first.
Search WWH ::

Custom Search