# Using the output_variables Argument

WARNING

Requires Redux v4.0.3+

The argument is related to the output argument, but has a slightly different focus. The purpose is to provide dynamic variables in a way that can be used by standard CSS as well as LESS/SCSS compilers.

All generated variables will use the following naming strategy:

  • If field produces an array: {prefix}{field_id}-{key}
  • If field produces an string: {prefix}{field_id}

The {prefix} will vary depending on the method you are using output_variables as. Namely as CSS variables or using the compiler hook with a SCSS/LESS compiler.

To enable, add this code 'output_variables' => true to the section or field object you want. If set to the section level, all fields that can be used will have the value applied.

# Supported Arguments

Name
Type Default Description
output_variables boolean false Can be set at the section or field object levels. When set to true, values will be appended to generated CSS.
ouput_variables_prefix string -- Can be set at the global args, section, or field object levels.
compiler_ouput_variables_prefix string $ For use with the compiler hook. An array of output variables is passed to the compiler hook.

# Using with CSS Variables

CSS Variables are a powerful way to use modern design practices and CSS. By using them, your CSS line count can be greatly reduced. Redux can auto-generate these variables and append them to the :root{} selector of your page dynamically.

See the below Example Generated CSS Variables for example use.

# Using with the Compiler Hook

WARNING

This is a dangerous approach since PHP based SCSS/LESS compilers are quite memory intensive. Users on shared hosting environments will have issues. It is STRONGLY advised to use CSS variables for greater stability.

If you prefer to use a LESS/SCSS compiler you may want to use generated variables to prepend to your SCSS files. To enable this option, the Redux compiler hook will return an array of prepared variables. You may need to alter the prefix to match your compiler of choice.

# Changing the Compiler Variables Prefix

By default the global argument compiler_ouput_variables_prefix is set to $ which works for SCSS. If you are using LESS, you will need to run the following to properly generate your variables in a way that can be used by your compiler. By so doing, your variables will be properly prefixed with the @ symbol which is what LESS requires.

Redux::setArgs( 'YOUR_OPT_NAME', array( 'compiler_ouput_variables_prefix' => '@' ) );

# Example Config

For this example, we'll use the border field, found in the sample-config.php. To pass only a CSS selector and have Redux generate the CSS, use the output argument instead.

array(
    'id'        => 'opt-header-border',
    'type'      => 'border',
    'title'     => __('Header Border Option', 'redux-framework-demo'),
    'subtitle'  => __('Subtitle goes here', 'redux-framework-demo'),
    'output_variables' => true,
    'desc'      => __('This is the description field.', 'redux-framework-demo'),
    'default'   => array(
        'border-color'  => '#1e73be', 
        'border-style'  => 'solid', 
        'border-top'    => '3px', 
        'border-right'  => '3px', 
        'border-bottom' => '3px', 
        'border-left'   => '3px'
    )
),

# Example Generated CSS Variables

WARNING

Redux will generate these variables on a single line. The example below is expanded for display purposes only.

:root {
    --site-header-border-top: 3px solid #1e73be;
    --site-header-border-right: 3px solid #1e73be;
    --site-header-border-bottom: 3px solid #1e73be;
    --site-header-border-left: 3px solid #1e73be;
}

To use a generated variable for a field with ID header_color of field type color, you would use the following in your CSS files:

.header_selector {
    color: var( --header_color, "#fff" );
}

TIP

The second value is CSS val() function is the fallback value and is not required.

# Example Generated Compiler Variables

WARNING

This is a dangerous approach since PHP based SCSS/LESS compilers are quite memory intensive. Users on shared hosting environments will have issues. It is STRONGLY advised to use CSS variables for greater stability.

Below is a full example of a compiler hook to generate your SCSS/LESS on save. The content of $output_variables will be populated with the following provided the above Example Config is used.

array(
    '$opt-typography-body-color' => '#dd9933',
    '$opt-typography-body-font-size' => '30px'
    '$opt-typography-body-font-family' => 'Arial, Helvetica, sans-serif'
    '$opt-typography-body-font-weight' => 'Normal'
)

TIP

By default the global argument compiler_ouput_variables_prefix is set to work with SCSS. See Changing the Compiler Variables Prefix to change the prefix to your compiler's requirements.

function your_css_compiler( $options, $css, $changed_values, $output_variables ) {
    $variables = join( ";\n", $output_variables );
    // TODO: Append $variables to your SCSS/LESS and compile then write out.
}
add_action( 'redux/options/YOUR_OPT_NAME/compiler', 'your_css_compiler', 10, 4 ); // The integer 4 is important to get the extra variables.

# Using output_variables with the required Argument

If a field is not "visible" due to an unmet required statement, the generated CSS variables for fields in this state will not be appended to the page. By so doing the output is easier to manage. Be mindful of this when using these two arguments together.