Last active
June 20, 2018 19:26
-
-
Save Phoenix2k/9784e325b9627ff8d84c60aa87f5ba92 to your computer and use it in GitHub Desktop.
WordPress: Inline stylesheet in <head>
This file contains hidden or 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 | |
/** | |
* Prints out entire stylesheet in the <head> section of the site. | |
* Supports replacing relative URLs | |
*/ | |
add_action( 'wp_head', function() { | |
$stylesheet = get_stylesheet_directory() . '/style.min.css'; | |
if ( ! file_exists( $stylesheet ) ) return echo '<!--404-CSS-->'; | |
$stylesheet = file_get_contents( $stylesheet ); | |
$relative_urls = array( | |
'fonts/' => get_stylesheet_directory_uri() . '/fonts/', | |
'img/' => get_stylesheet_directory_uri() . '/img/', | |
'js/' => get_stylesheet_directory_uri() . '/js/' | |
); | |
foreach ( $relative_urls as $relative_url => $theme_url ) { | |
$stylesheet = str_replace( 'url(' . $relative_url, 'url(' . $theme_url, $stylesheet ); | |
} | |
printf( "\n" . '<style>%s</style>' . "\n", $stylesheet ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment