Skip to content

Instantly share code, notes, and snippets.

@Phoenix2k
Last active June 20, 2018 19:26
Show Gist options
  • Save Phoenix2k/9784e325b9627ff8d84c60aa87f5ba92 to your computer and use it in GitHub Desktop.
Save Phoenix2k/9784e325b9627ff8d84c60aa87f5ba92 to your computer and use it in GitHub Desktop.
WordPress: Inline stylesheet in <head>
<?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