Support

Account

Home Forums Backend Issues (wp-admin) Suggestions for creating dynamic stylesheets with ACF values

Solving

Suggestions for creating dynamic stylesheets with ACF values

    • Xav

    • September 27, 2019 at 5:04 am

    Hello,

    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:

    1. I don’t want to write the values directly to template files like: <h2 style="color: <?php the_field("heading_colour"); ?>">.
    2. I don’t want to create style blocks in the header file
    3. I ideally don’t want to re-save the file on page load, only on update – whether it’s via an Options page or updating a single page/post that uses ACF

    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>’

    so:

    1. Decreased Page Load Time
    2. Decreased Site Dev Time
    3. Decreased Future Dev Time

    now outweigh my need to separate all CSS into stylesheets and all JavaScript into their own files, etc.

    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.

    • Xav

    • September 29, 2019 at 11:00 pm

    Hi John,

    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!

    Thanks John

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

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.