Created
July 14, 2021 06:34
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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