This is a simple ACF Add-on field allowing the creation of color swatches that behave as radio buttons.
License: GPLv2 or later, (*1)
This is a simple ACF Add-on field allowing the creation of color swatches that behave as radio buttons. This is particularly useful if you want to limit the color options available to the end users, instead of using a color picker field., (*2)
This is an add-on for the Advanced Custom Fields WordPress plugin and will not provide any functionality to WordPress unless Advanced Custom Fields is installed and activated., (*3)
The color swatch field is a radio button field, with a few modifications. The syntax for building the choices is the same as a radio button field, in that you may include a key : value pair, or just a single value, each option separated by a line break., (*4)
The field expects each line to be a color string, and can interpret all of the possible color formats., (*5)
For example, all of the following will produce a bright red swatch:, (*6)
red
#ff0000
rgb(255,0,0)
rgba(255,0,0, 1)
hsl(0,100%,50%)
hsla(0,100%,50%, 1)
Additionally, if you are using rgb/rgba or hsl/hsla, you may use only the values, if desired. For instance:, (*7)
255,0,0, 1 // will be recognized as rgba
0,100%,50% // will be recognized as hsl
This may be useful for defining CSS linear-gradients, or other situations where you may want to alter some of the values in the view., (*8)
Note that while the field can recognize these shortened syntaxes to display the color swatch in the Wordpress back end, it will still output only what you entered when using the_field()
or get_field()
., (*9)
, (*10)
, (*11)
https://wordpress.org/plugins/acf-color-swatches, (*12)
If using Composer (e.g. with Bedrock)
* Add repo to composer.json
:, (*13)
"repositories": [ { "type": "git", "url": "https://github.com/nickforddesign/acf-swatch" } ]
composer require nickford/acf-swatch
Assuming you select a swatch with value "rgba(255,0,0, 1)"
:, (*14)
As inline CSS:, (*15)
<section style="background-color: <?php the_field('swatches')?>">
Inside a <style>
tag:, (*16)
<style> section { background-color: <?php the_field('swatches')?>; } </style>
One situation where you might want to take advantage of the shorthand syntax would be to control a CSS linear-gradient that fades a color from 100% opacity to 0%., (*17)
Assuming you select a swatch with value "255,0,0"
:, (*18)
<style> section { background: linear-gradient( to bottom, rgba(<?php the_field('swatches')?>, 1) 0%, rgba(<?php the_field('swatches')?>, 0) 100% ); } </style>
Take advantage of the acf/swatch_settings/path
and acf/swatch_settings/url
filters to modify where acf-swatch is located so that js and css are found correctly in the backend., (*19)
/** * Include ACF Color Swatch Field */ add_filter('acf/swatch_settings/path', 'my_swatch_path', 10, 1); function my_swatch_path( $path ) { $path = get_template_directory() . '/path/to/acf-swatch'; return $path; } add_filter('acf/swatch_settings/url', 'my_swatch_url', 10, 1); function my_swatch_url( $url ) { $url = get_template_directory_uri() . '/path/to/acf-swatch'; return $url; } include( 'path/to/acf-swatch.php' );