How to wp enqueue style in WordPress function.php

Categories:

To enqueue a style in WordPress functions.php file, you can use the wp_enqueue_style function, which adds the stylesheet to the queue of stylesheets that will be loaded on the page. Here’s an example:

function mytheme_enqueue_styles() {
wp_enqueue_style( 'mytheme-style', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_styles' );

In this example, we’re using wp_enqueue_style to add the stylesheet with the handle 'mytheme-style'. We’re also using the get_stylesheet_uri function to get the URI for the current theme’s stylesheet.

how to wp enqueue style in wordpress function.php

The add_action function is used to specify when the mytheme_enqueue_styles function should be called. In this case, we’re using the 'wp_enqueue_scripts' action, which is called on the front-end when scripts and styles are enqueued.

You can also specify additional arguments to wp_enqueue_style, such as the path to the stylesheet file, dependencies, version number, and media type. Here’s an example that uses some of these options:

function mytheme_enqueue_styles() {
wp_enqueue_style( 'mytheme-style', get_template_directory_uri() . '/style.css', array(), '1.0.0', 'all' );
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_styles' );

In this example, we’re enqueuing a stylesheet with the handle 'mytheme-style', located in the theme’s directory at style.css. We’re not specifying any dependencies, and we’re using version number 1.0.0. We’re also specifying the media type as 'all', which means the stylesheet will be used for all devices.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *