Skip to content

Instantly share code, notes, and snippets.

@dingo-d
Created July 14, 2021 06:34
Show Gist options
  • Save dingo-d/261d6be467c1ee16493f244f1f60a2d2 to your computer and use it in GitHub Desktop.
Save dingo-d/261d6be467c1ee16493f244f1f60a2d2 to your computer and use it in GitHub Desktop.
Color picker meta that adds inline css variable using ACF color palette
// Only run if ACF is active.
if (typeof acf !== 'undefined') {
acf.add_filter('color_picker_args', function (args, $field) {
// Add predefined color palette.
args.palettes = ['#5D2BFF', '#FFE194', '#E45353', '#96DFFF', '#FFBDA7', '#8CEAB7', '#ECA7C8'];
return args;
});
}
if (class_exists(ACF::class)) {
\add_action('wp_enqueue_scripts', [$this, 'addInlineGlobalVariables']);
}
/**
* Method that adds inline global css variables
*
* @return void
*/
public function addInlineGlobalVariables(): void
{
$handle = "{$this->getAssetsPrefix()}-theme-styles";
global $post;
$pageId = $post->ID;
$pageColor = get_field(MetaFields::PAGE_COLOR, $pageId) ?? '#111111';
$customCss = ":root{--global-page-color: {$pageColor}}";
\wp_add_inline_style($handle, $customCss);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment