wp_enqueue_style()

How to enqueue the style using wp_enqueue_style

by Expert of Tech

In this paragraph, wp_enqueue_style() is an important and basic function when you developing a WordPress theme. Firstly, You must know about wp_enqueue_style function. Normally we add all stylesheets in the head tags but in WordPress, you have to use wp_enqueue_style(). However, I will show you the function details below. So, you must write the function on functions.php into the theme folder.

wp_enqueue_style()

wp_enqueue_style( string $handle, string $src = '', string[] $deps = array(), string|bool|null $ver = false, 
string $media = 'all' )

Parameters 

$handle:

(string) (Required) Name of the stylesheet. Should be unique.

$src:

(string) (Optional) a full URL of style relative of WordPress root directory.

$deps:

(string[]) (Optional) An array of the registered stylesheets that maintains the order of all stylesheets.

$ver

(string|bool|null) (Optional) String specifying stylesheet version. It useful for cache issues. when you change any part of the stylesheet and change the version number so the changes affect the page at the same time.If the version is set to false, a version number is automatically added equal to the current installed WordPress version. If set to null, no version is added.

$media

(string) (Optional) The media for which this stylesheet has been defined. Accepts media types like ‘all’, ‘print’ and ‘screen’, or media queries like ‘(orientation: portrait)’ and ‘(max-width: 640px)’.

function my_theme_enqueue_styles() {
 
    $parent_style = 'jobcareertheme';
 
    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( $parent_style ),
        wp_get_theme()->get('1.0.0')
    );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
wp_enqueue_style('main-styles', get_template_directory_uri() . '/css/style.css', array(), filemtime(get_template_directory() . '/css/style.css'), false);
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
function my_scripts_method(){

	wp_enqueue_script(
		'custom_script',
		get_template_directory_uri() . '/js/custom_script.js',
		['jquery']
	);
}

Moreover, get_template_directory_uri() is an another important function when you using wp_enqueue_style. so, get_temlate_directory_uri() give you root directoy of parent template.

In conclusion, That’s all about wp_enqueue_style. But, if you face any difficulties so feel free to contact one of our experts or email me. I will respond ASAP. To learn more about wordpress speed optimization click here.