Using WordPress ‘print_styles_array’ PHP filter

‘print_styles_array’ is a WordPress PHP filter that allows you to modify the array of enqueued styles before they are processed for output. This filter can be useful when you want to modify, reorder, or remove specific styles from being loaded on your site.

Usage

To use this filter, you need to create a custom function and hook it to the ‘print_styles_array’ filter using the add_filter() function.

function custom_print_styles_array( $to_do ) {
    // Your custom code here
    return $to_do;
}
add_filter( 'print_styles_array', 'custom_print_styles_array' );

Parameters

  • $to_do (string[])
    • The list of enqueued style handles about to be processed.

Examples

Remove a specific style

function remove_specific_style( $to_do ) {
    $key = array_search( 'style-to-remove', $to_do );
    if ( false !== $key ) {
        unset( $to_do[ $key ] );
    }
    return $to_do;
}
add_filter( 'print_styles_array', 'remove_specific_style' );

This code snippet removes the ‘style-to-remove’ handle from the enqueued styles array, preventing it from being loaded on the site.

Change the order of styles

function reorder_styles( $to_do ) {
    $style_to_move = 'style-to-move';
    $new_position = 2;

    if ( in_array( $style_to_move, $to_do ) ) {
        $to_do = array_diff( $to_do, [ $style_to_move ] );
        array_splice( $to_do, $new_position, 0, $style_to_move );
    }

    return $to_do;
}
add_filter( 'print_styles_array', 'reorder_styles' );

This code snippet changes the position of the ‘style-to-move’ handle within the enqueued styles array, effectively changing its loading order.

Add a new style to the array

function add_new_style( $to_do ) {
    $new_style = 'new-style';
    if ( ! in_array( $new_style, $to_do ) ) {
        array_push( $to_do, $new_style );
    }
    return $to_do;
}
add_filter( 'print_styles_array', 'add_new_style' );

This code snippet adds the ‘new-style’ handle to the enqueued styles array, making it load on the site.

Conditionally remove a style on specific pages

function remove_style_on_specific_pages( $to_do ) {
    if ( is_page( 'about' ) ) {
        $key = array_search( 'style-to-remove', $to_do );
        if ( false !== $key ) {
            unset( $to_do[ $key ] );
        }
    }
    return $to_do;
}
add_filter( 'print_styles_array', 'remove_style_on_specific_pages' );

This code snippet removes the ‘style-to-remove’ handle from the enqueued styles array only on the ‘about’ page, preventing it from being loaded on that specific page.

Keep specific styles only on certain post types

function keep_styles_on_post_type( $to_do ) {
if ( ! is_singular( 'custom_post_type' ) ) {
$styles_to_keep = [ 'style-1', 'style-2' ];
$to_do = array_intersect( $to_do, $styles_to_keep );
} return $to_do; } add_filter( 'print_styles_array', 'keep_styles_on_post_type' );

This code snippet filters the enqueued styles array to only include ‘style-1’ and ‘style-2’ when the current page is not a single view of the ‘custom_post_type’ post type.