Skip to content

Instantly share code, notes, and snippets.

@yratof
Last active December 1, 2020 19:41
Show Gist options
  • Save yratof/1cf49598c1b26cbe20e6 to your computer and use it in GitHub Desktop.
Save yratof/1cf49598c1b26cbe20e6 to your computer and use it in GitHub Desktop.
Using ACF to create a stylesheet
<?php /* Brand Colour */ $colour = get_field( 'colour', 'option' ); ?>
.brand--background{ background-color: <?php echo $colour ?>; }
.brand--colour{ color: <?php echo $colour ?>; }
.brand--border{ border-color: <?php echo $colour ?>; }
<?php
/**
* Generate stylesheet for Branding when ACF options are saved
*/
function generate_options_css() {
$ss_dir = get_stylesheet_directory();
ob_start(); // Capture all output into buffer
require($ss_dir . '/assets/branding-css.php'); // Grab the custom style php file
$css = ob_get_clean(); // Store output in a variable, then flush the buffer
file_put_contents($ss_dir . '/assets/css/brand.css', $css, LOCK_EX); // Save it as a css file
}
add_action( 'acf/save_post', 'generate_options_css' ); //Parse the output and write the CSS file on post save
/**
* Enqueue stylesheet in wp_head
*/
add_action( 'wp_head', 'branding_styles' );
function branding_styles(){
wp_enqueue_style( 'branding-css', get_stylesheet_directory_uri() . '/assets/css/brand.css' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment