WordPress Dynamic CSS

Description

WordPress Dynamic CSS is a lightweight library for generating CSS stylesheets from dynamic content (i.e. content that can be modified by the user).
The most obvious use case for this library is for creating stylesheets based on Customizer options.
Using the special dynamic CSS syntax you can write CSS rules with variables that will be replaced by static values using a custom callback function that you provide.

As of version 1.0.2 this plugin supports multiple callback functions, thus making it safe to use by multiple plugins/themes at the same time.

Basic Example

First, add this to your functions.php file:

// 1. Load the library (skip this if you are loading the library as a plugin)
require_once 'wp-dynamic-css/bootstrap.php';

// 2. Enqueue the stylesheet (using an absolute path, not a URL)
wp_dynamic_css_enqueue( 'my_dynamic_style', 'path/to/my-style.css' );

// 3. Set the callback function (used to convert variables to actual values)
function my_dynamic_css_callback( $var_name )
{
    return get_theme_mod($var_name);
}
wp_dynamic_css_set_callback( 'my_dynamic_style', 'my_dynamic_css_callback' );

// 4. Nope, only three steps

Then, create a file called my-style.css and write this in it:

body {
    background-color: $body_bg_color;
}

In the above example, the stylesheet will be automatically compiled and printed to the of the document. The value of $body_bg_color will be replaced by the value of get_theme_mod('body_bg_color').

Now, let’s say that get_theme_mod('body_bg_color') returns the value #fff, then my-style.css will be compiled to:

body {
    background-color: #fff;
}

There’s even support for array subscripts and piped filters:

body {
    background-color: $myVar['index'];
    color: $myVar|myFilter;
}

You can find detailed documentation on how to use this library on the GitHub page

Useful Links

Installation

Please follow the instructions on the plugin’s GitHub page for detailed explanation and examples.

Reviews

Easiest Dynamic Css Generation

I was having a little brain stress over having to learn Sass, getting started was probably the issue. But WordPress Dynamic CSS takes all of that away, with a light framework or compiler for dynamic css.

Best part is that I don’t have to learn a new language just for this reason and I also can write css like I normally would, just replacing colors, etc. with variables.

Super simple!

NOTE: I didn’t know this and had problems getting this to work at first, but that is because the variables need to be all lowercase. Capital letters were leaving me with an empty variable output.

Read all 1 review

Contributors & Developers

“WordPress Dynamic CSS” is open source software. The following people have contributed to this plugin.

Contributors

Changelog

1.0.5

  • (NEW) Added support for cache
  • (NEW) Added support for piped filters
  • (FIX) Separated enqueued stylesheets
  • (FIX) Increased priority of enqueued stylesheets to override static stylesheets

1.0.4

  • (FIX) Set cache-control to no-cache so that changes to options are reflected immediately
  • (NEW) Added support for CSS minification

1.0.3

  • (NEW) Added support for variable subscripts

1.0.2

  • (NEW) Added support for multiple callback functions
  • (FIX) The library is now safe to use by multiple plugins/themes in the same installation

1.0.1

  • (NEW) Added support for loading compiled CSS as an external stylesheet

1.0.0

  • Initial release