Skip to content

Instantly share code, notes, and snippets.

@mauryaratan
Last active December 17, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauryaratan/5570489 to your computer and use it in GitHub Desktop.
Save mauryaratan/5570489 to your computer and use it in GitHub Desktop.
Custom CSS file generation in WordPress
<?php
// OUTPUT CUSTOM CSS FILE
function stag_link_custom_styles(){
$output = '';
if(apply_filters('stag_custom_styles', $output)){
$perma_structure = get_option('permalink_structure');
$url = home_url(). '/stag-custom-styles.css?'.time();
if(!$perma_structure) $url = home_url(). '?page_id=stag-custom-styles.css';
echo '<link rel="stylesheet" href="'.$url.'" type="text/css" media="screen" />';
}
}
add_action('wp_head', 'stag_link_custom_styles', 15);
// CREATE CUSTOM CSS FILE
function stag_create_custom_styles(){
$perma_structure = get_option('permalink_structure');
$show_css = false;
if($perma_structure){
if(!isset($_SERVER['REQUEST_URI'])){
$_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'], 1);
if(isset($_SERVER['QUERY_STRING'])){$_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING'];}
}
$url = (isset($GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI'])) ? $GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI'] : $_SERVER['REQUEST_URI'];
if(preg_replace('/\\?.*/', '', basename($url)) == 'stag-custom-styles.css') $show_css = true;
}else{
if(isset($_GET['page_id']) && $_GET['page_id'] == 'stag-custom-styles.css' ) $show_css = true;
}
if($show_css){
$output = '';
header('Content-Type: text/css');
echo apply_filters('stag_custom_styles', $output);
exit;
}
}
add_action('init', 'stag_create_custom_styles');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment