Created
February 28, 2022 14:09
-
-
Save dustinleer/c1a440d0cbe3c2218b3511e72629b4e7 to your computer and use it in GitHub Desktop.
Removes the !important
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Lowering specificity of WordPress global styles. | |
*/ | |
add_action( 'init', function() { | |
// WP5.9+ only. | |
if ( ! function_exists( 'wp_get_global_stylesheet' ) ) { | |
return; | |
} | |
// Dequeue original WP global styles. | |
remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' ); | |
// Enqueue WP global styles early. | |
add_action( 'wp_enqueue_scripts', function() { | |
// Lower CSS code specificity. | |
$stylesheet = str_replace( [ 'body', '!important' ], [ ':root', '' ], wp_get_global_stylesheet() ); | |
if ( empty( $stylesheet ) ) { | |
return; | |
} | |
wp_register_style( 'wp-global-styles', false ); | |
wp_add_inline_style( 'wp-global-styles', $stylesheet ); | |
wp_enqueue_style( 'wp-global-styles' ); | |
}, 0 ); | |
// Treat also editor styles. | |
add_filter( 'block_editor_settings_all', function( $editor_settings ) { | |
// Lower CSS code specificity. | |
$editor_settings['styles'] = array_map( function( $style ) { | |
if ( ! empty( $style['css'] ) ) { | |
$style['css'] = str_replace( [ 'body', '!important' ], [ ':root', '' ], $style['css'] ); | |
} | |
return $style; | |
}, $editor_settings['styles'] ); | |
return $editor_settings; | |
} ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment