Using WordPress ‘print_late_styles()’ PHP function

The print_late_styles() WordPress PHP function prints the styles that were queued too late for the HTML head.

Usage

To use the print_late_styles() function, simply call it without any parameters.

print_late_styles();

Parameters

  • None

More information

See WordPress Developer Resources: print_late_styles

Examples

In this example, we’ll create a function to enqueue a custom style, and then use wp_footer action to print the late styles.

// Enqueue custom style for footer
function my_custom_footer_style() {
    wp_enqueue_style('custom-footer', get_stylesheet_directory_uri() . '/custom-footer.css', array(), '1.0.0', 'all');
}
add_action('wp_enqueue_scripts', 'my_custom_footer_style');

// Add print_late_styles to wp_footer action
add_action('wp_footer', 'print_late_styles');

Late-enqueuing a Google Font

This example shows how to late-enqueue a Google Font and print it using print_late_styles().

// Enqueue Google Font
function my_google_font() {
    wp_enqueue_style('google-font', 'https://fonts.googleapis.com/css?family=Roboto&display=swap', array(), null);
}
add_action('wp_enqueue_scripts', 'my_google_font', 20);

// Print late styles
add_action('wp_footer', 'print_late_styles');

Late-enqueuing an admin style

In this example, we’ll late-enqueue an admin style and print it using print_late_styles().

// Enqueue admin style
function my_admin_style() {
    wp_enqueue_style('my-admin', get_template_directory_uri() . '/admin-style.css', array(), '1.0.0', 'all');
}
add_action('admin_enqueue_scripts', 'my_admin_style', 20);

// Print late styles
add_action('admin_footer', 'print_late_styles');

Using a conditional tag to late-enqueue a style

This example demonstrates how to late-enqueue a style using a conditional tag and print it with print_late_styles().

// Conditionally enqueue style for single posts
function my_single_post_style() {
    if (is_single()) {
        wp_enqueue_style('single-post', get_template_directory_uri() . '/single-post.css', array(), '1.0.0', 'all');
    }
}
add_action('wp_enqueue_scripts', 'my_single_post_style', 20);

// Print late styles
add_action('wp_footer', 'print_late_styles');

Late-enqueuing a style for a specific page template

In this example, we’ll late-enqueue a style for a specific page template and print it using print_late_styles().

// Enqueue style for a specific page template
function my_page_template_style() {
    if (is_page_template('template-custom.php')) {
        wp_enqueue_style('custom-template', get_template_directory_uri() . '/custom-template.css', array(), '1.0.0', 'all');
    }
}
add_action('wp_enqueue_scripts', 'my_page_template_style', 20);

// Print late styles
add_action('wp_footer', 'print_late_styles');