Home › Forums › General Issues › PHP as CSS with ACF field › Reply To: PHP as CSS with ACF field
I’ve used the below which grabs values from an option page, then creates a CSS file and enqueues it:
###################################################
# Create Custom CSS
###################################################
function acf_generate_options_css() {
$ss_dir = get_stylesheet_directory();
ob_start(); // Capture all output into buffer
require($ss_dir . '/inc/custom-styles.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 . '/css/custom-styles.css', $css, LOCK_EX); // Save it as a css file
}
add_action( 'acf/save_post', 'acf_generate_options_css', 20 );
I then enqueue the script:
function acf_enqueue_scripts() {
wp_enqueue_style( 'custom-theme-css', get_template_directory_uri() . '/css/custom-styles.css' );
}
add_action( 'wp_enqueue_scripts', 'acf_enqueue_scripts', 99 );
And custom-styles.php looks like this:
$colour_name = get_field('colour_name','option');
$select_colour = get_field('select_colour','option');
.bg-colour-<?php echo str_replace(" ","-",strtolower($colour_name)); ?> {
background-color: <?php echo $select_colour; ?>;
}
Not sure if that helps at all?
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF’s Repeater field is a versatile solution for repeating content in #WordPress.
— Advanced Custom Fields (@wp_acf) December 29, 2022
What’s your favorite use case for the Repeater field?
What’s the weirdest thing you’ve ever used it for?
Let us know!👇
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.