Script and Style
wp_register_script() ----------Registers the js file.
wp_enqueue_script() ----------Registers and connects the js file.
wp_dequeue_script() ----------Deletes file from the output queue.
wp_add_inline_script() ----------Adds JS code to the registered script.
wp_deregister_script() ----------Unregisters the js file.
wp_script_add_data() ----------Adds data to the script. Example: script only for "IE 6", "lt IE 9".
wp_localize_script() ----------Adds data before the specified script.
wp_script_is() ----------Whether the file has been registered/waiting for output.
wp_register_style() ----------Registers the css file.
wp_enqueue_style() ----------Registers and connects the css file.
wp_dequeue_style() ----------Deletes file from the output queue.
wp_add_inline_style() ----------Adds styles directly to the html document.
wp_deregister_style() ----------Unregisters the css file.
wp_style_add_data() ----------Adds data to styles. Example: styles only for "IE 6", "lt IE 9".
wp_style_is() ----------Whether the file has been registered/waiting for output.
add_action( 'wp_enqueue_scripts', 'add_my_scripts' ); // Front
add_action( 'admin_enqueue_scripts', 'add_my_scripts' ); // Admin
add_action( 'login_enqueue_scripts', 'add_my_scripts' ); // wp-login.php
function add_my_scripts(){
if ( ! wp_script_is( 'my-script', 'enqueued' ) ) {
// The my-script is not added to the queue
}
if ( ! wp_style_is( 'my-style', 'registered' ) ) {
// Styles my-script is not registered
}
wp_enqueue_script( 'my-script', 'src', ['deps'], '1.0', 'in_footer' );
wp_enqueue_style( 'my-style', 'src', ['deps'], '1.0', 'all' );
wp_enqueue_style( 'theme-style', get_stylesheet_uri() ); // theme style.css
wp_localize_script( 'my-script', 'myajax', [
'ajaxurl' => admin_url( 'admin-ajax.php' )
] );
wp_script_add_data( 'my-script', 'conditional', 'lt IE 9' );
wp_style_add_data( 'my-style', 'conditional', 'lt IE 9' )
wp_add_inline_script( 'my-scripts', 'alert("Hello!");' );
wp_add_inline_style( 'my-style', '
.mycolor{
background: #fff;
}
');
wp_deregister_script( 'my-script' );
wp_deregister_style( 'my-style' );
}