Using stylesheet and script to make your website beautiful and attractive. Also, it is important to how to add these script and stylesheet to WordPress. Most of the time we all are added it in header and footer but we can also use WP enqueue script functionality to add scripts and stylesheets in WordPress.
Enqueueing is a CMS-friendly way of adding scripts and styles to WordPress websites. Using enqueueing functionality, WordPress can be linking this stylesheet and script in the header and footer. You can even specify the dependencies of your scripts and stylesheets and WordPress will add them in the correct order.
You can add this enqueueing or Dequeueing code in your theme’s functions.php file of your active child theme (or theme) or plugin file.
<pre>
wp_enqueue_script( $handle, $source, $dependencies, $version, $in_footer );
wp_enqueue_style( $handle, $source, $dependencies, $version, $media );
</pre>
<pre>
function ls_add_myscripts(){
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/script.js');
wp_enqueue_style( 'style-name', get_template_directory_uri() . '/custom.css');
}
add_action('wp_enqueue_scripts', ' ls_add_myscripts');
</pre>
Technically scripts and styles are first registered and then enqueued. The difference between registration and enqueuing is, registration lets WordPress know about your scripts or stylesheet, while enqueuing actually adds them to the page.
<pre>
function ls_add_myscripts() {
wp_register_script( 'script-name', get_stylesheet_directory_uri() . '/script.js', array( 'jquery' ) );
wp_enqueue_script('script-name'); }
add_action( 'wp_enqueue_scripts', ' ls_add_myscripts' );
</pre>
WordPress provides dequeueing and deregistering functions for both scripts and styles.
<pre>
function dequeue_unnecessary_styles() {
wp_dequeue_style( 'bootstrap-map' );
wp_deregister_style( 'bootstrap-map' );
}
</pre>
<pre>
function dequeue_unnecessary_scripts() {
wp_dequeue_script('bootstrap-js' );
wp_deregister_script('bootstrap-js' );
}
</pre>
I hope this post is useful for you. If you have any query than connect our wordpress developer in India at Lathiya Solutions, Surat.