Archived topic
How to update GenerateBlocks colors programatically?
1 reply · Started by Alex on August 19, 2021
I'm a developer working on a child theme that includes a feature that allows a user to set generate some colors, and have these affect the entire theme. What is the correct way to update theme and GenerateBlocks colors with code? This would likely be coming from customize_controls_enqueue_scripts. Also, what is the correct way to set GenerateBlock default colors.
I have also been playing with the 3.1.0 beta, which has Global Colors (great feature!). What is the proper way to set these variables with code? Also, currently the Global Colors are present in Blocks (the colors aren't accessable, but I'm sure they'll be eventually), is there a way(/what will be the way) to set blocks to Global Colors by default and update existing blocks?
Thank you!
Hi there,
What is the correct way to update theme and GenerateBlocks colors with code?
This really depends on what you're targeting. If you're allowing the user to pick colors and apply them to certain elements, you'll need to write PHP to build the necessary CSS using the correct selectors and your color values.
What is the proper way to set these variables with code?
You can set the defaults using the generate_option_defaults filter: https://github.com/tomusborne/generatepress/blob/release/3.1.0/inc/defaults.php#L69
is there a way(/what will be the way) to set blocks to Global Colors by default and update existing blocks?
GenerateBlocks also has filterable defaults: https://github.com/tomusborne/generateblocks/blob/1.3.5/includes/defaults.php
Be careful there though, as some defaults (mainly numbers) make it so the user can't clear the value. For example, 40px of padding default would mean the user can never leave an empty field (it will revert back to the default).
Hope this helps!