This problem has perplexed me for years and I’ve yet to find what looks like a reliable, flexible, non-hack solution to it.
I want to be able to allow a client to set colour values using ACF fields (either in an Options page or even in individual pages/posts) and then pull those values into a dynamic CSS file and load it the normal enqueue way.
These are the boundaries I want to work within:
<h2 style="color: <?php the_field("heading_colour"); ?>">.
Has anyone come attempted this before and come up with a reasonable solution? There is a blog post here that appears to do most of the above, but it’s now over 6 years old and I wondered if there was a newer, more bulletproof solution out there.
Would really like to find an answer to this. Any ideas?
Thanks for any help.
I took a quick look at the article you linked and I don’t see anything in the basic idea that would not still apply. If I was going to do this that’s likely the way I would go.
What follows is just my thinking on the subject and you can disregard it if you want. It’s just an opinion and you know what they say about opinions.
I, personally, have given up on my arguments that were the same as yours because I was determined at one point in my life to keep complete separation between content and design. Mostly now I use inline styles when I need to. I either put the style right in the element or I generate inline
<style> blocks when I need to do something complicated (like add a client uploaded image as a background using responsive background image CSS.) Why, because it takes less time to load a page with inline styles than it does to load that extra dynamic stylesheet, even if that extra dynamic stylesheet id pre-generated.
In your case you’re probably talking about 2 pre-generated style sheets, one using global options and another for page specific options, further increasing the number of http requests. Everything is about page load time now and even knocking off a few milliseconds off can be important. Gone are the days when you disregard these milliseconds in favor of “Best Practices”
I am also usually under time constraints. Pre-generation of separate style sheets would take more time to build a site, extra time that most of my clients would rather not pay for. In addition to this, in 6 months I’ll need to figure out WTF I was doing. It means changes in multiple files where using inline styles lets me see everything at once.
I still use stylesheets for everything that will not change, but inline styles for those things that I want to be configurable.
My change of opinion happened when it became possible to add a ‘<style>’ block anywhere in a page and was no longer confined to needing to be in the `<head>’
Let’s look at it another way. I am building a site that uses bootstrap. I am using flexible content fields. I am giving the client the ability to adjust the bootstrap columns. What is the difference if I dynamically include a class like “align-self-bottom” or I add a style like “color: #000” to an element? Not much.
Thank you for your considered feedback. It’s always good to hear others’ opinions as we all know it’s very easy to get tunnel vision and end up down rabbit holes in the world of dev.
I absolutely agree that page load times are critical. My ultimate goal was to generate these stylesheets and then concatenate them into a single one to reduce http requests. However, inline or stylesheet blocks would clearly still be quicker.
My worry about placing stye tags throughout the document (not just in the head) would be flashes of unstyled content before the DOM has fully loaded. Though I guess it depends what those style tags are targeting as to whether it’s worth the tradeoff.
I think for now I might have to settle on header style blocks as this seems a happy medium and I think you’re right, me in 6 months will look back and probably hate now now for trying to be too clever!
Hi – here is the way I do it.
1. Create an option page – something like “Theme Settings”
2. Add Fields for each css style I need
3. Group them in tabs for for mobile/tablet/desktop/HD
4. Define a hook which is fired once the option page is updated
5. The hook is generating a css file / it updates the css file
6. include the css file in your theme in function.php
If you want to go more advanced. Create a vars.less (I am using less and uikit) and compile the css with a less compiler, after the option page is saved. Like this I only load one css file
You must be logged in to reply to this topic.
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!
© 2022 Advanced Custom Fields.