- This topic has 29 replies, 8 voices, and was last updated 1 year ago by Ying.
-
AuthorPosts
-
October 27, 2021 at 9:28 am #1978882Gerard
Hi guys!
So before GP 3.1 we had the following filter to programmatically change the colors that were used in the customizer’s palette:
add_filter( 'generate_default_color_palettes', $array_of_colors );
This doesn’t work now since the new global colors system has been implemented.
Now, I would like to ask for a way to programmatically change the global colors. It should be a filter allowing us to set the CSS Variable Name and the color.
Maybe something like this?
add_filter( 'generate_global_colors', array( [ 'primary', '#000000'], [ 'secondary', '#000000'], [ 'accent', '#000000'], [ 'background', '#000000'], [ 'contrast', '#000000'], ) );
I rely on the previous filter in all of my client’s websites, since I have another global colors palette that syncs with GP and other plugins.
Would it be possible to add a filter like this as soon as possible?
Thank you,
GerardOctober 27, 2021 at 11:16 am #1979077Chris+1
October 27, 2021 at 1:12 pm #1979258LeoStaffCustomer SupportHi there,
I believe the
generate_option_defaults
filter should work:
https://github.com/tomusborne/generatepress/blob/adfe090929b0515cdf894f4c6b722cfe8c0790dc/inc/defaults.php#L20You can see the global colors section here:
https://github.com/tomusborne/generatepress/blob/adfe090929b0515cdf894f4c6b722cfe8c0790dc/inc/defaults.php#L69-L108Some examples of using the filter here:
https://generatepress.com/forums/search/generate_option_defaults/October 27, 2021 at 3:05 pm #1979414GerardHi Leo,
Thanks for your reply. I have some doubts though.
Would that really be considered a default? I want the colors to be overwritten by my code, even if the user tries to manually change them from Appearance, so I wouldn’t consider my request as how to set the default colors.
How would modifying a default value make them static so that users cannot modify my values?
October 27, 2021 at 7:49 pm #1979567TomLead DeveloperLead DeveloperHi there,
If you want to override the values completely, you can do this:
add_filter( 'option_generate_settings', function( $settings ) { $settings['global_colors'] = [ [ 'name' => __( 'Contrast', 'generatepress' ), 'slug' => 'contrast', 'color' => '#222222', ], [ 'name' => sprintf( __( 'Contrast %s', 'generatepress' ), '2' ), 'slug' => 'contrast-2', 'color' => '#575760', ], // And so on.. ]; return $settings; } );
Hope this helps!
October 28, 2021 at 3:46 am #1980089GerardHi Tom!
Thanks, that’s what I was looking for.
Just a last question. We have 3 key-value pairs:
- name
- slug
- color
The usage of the latter is obvious. As to the other two, should I understand that the slug is the CSS variable and that the name is the tooltip that appears when the color is on hover?
Thank you,
GerardOctober 28, 2021 at 5:05 am #1980216DavidStaffCustomer SupportHi there,
thats correct the slug name is what you see in the tooltip and is also what is used for the CSS variable.
eg.
slug' => 'contrast-2',
results in:
:root { --contrast-2: #575760; }
October 28, 2021 at 11:37 am #1981124GerardHi David,
Thanks for the clarification. What does the name do then if both the tooltip and the CSS variable are coming from the slug?
October 28, 2021 at 11:43 am #1981127TomLead DeveloperLead DeveloperIt’s just a required field when adding editor palettes in WordPress. We didn’t want to create an extra name field when adding colors, so we use the slug for both.
So adding a name is “required” but it doesn’t actually do anything.
October 29, 2021 at 11:23 am #1983135GerardIt’s all clear now, thanks Tom =)
October 29, 2021 at 8:15 pm #1983434TomLead DeveloperLead DeveloperNo problem!
February 16, 2022 at 9:04 am #2121494ElvisHello,
I have changed variable names to:primary-dark
primary-light
accent
secondary-light
secondary-darkI prefer these names as they better reflect the role of colors then contrast for example.
Now since I am doing thi in the very beginig everything seems to work, but If I click on the button “Default” it will change to Contrast-2.Is there a way to change the value of Default to primary-color?
ThanksFebruary 16, 2022 at 8:32 pm #2122087TomLead DeveloperLead DeveloperAbsolutely – you can do this:
add_filter( 'generate_option_defaults', function( $defaults ) { $defaults['global_colors'] = array( array( 'name' => __( 'Contrast', 'generatepress' ), 'slug' => 'contrast', 'color' => '#222222', ), array( /* translators: Contrast number */ 'name' => sprintf( __( 'Contrast %s', 'generatepress' ), '2' ), 'slug' => 'contrast-2', 'color' => '#575760', ), array( /* translators: Contrast number */ 'name' => sprintf( __( 'Contrast %s', 'generatepress' ), '3' ), 'slug' => 'contrast-3', 'color' => '#b2b2be', ), array( 'name' => __( 'Base', 'generatepress' ), 'slug' => 'base', 'color' => '#f0f0f0', ), array( /* translators: Base number */ 'name' => sprintf( __( 'Base %s', 'generatepress' ), '2' ), 'slug' => 'base-2', 'color' => '#f7f8f9', ), array( /* translators: Base number */ 'name' => sprintf( __( 'Base %s', 'generatepress' ), '3' ), 'slug' => 'base-3', 'color' => '#ffffff', ), array( 'name' => __( 'Accent', 'generatepress' ), 'slug' => 'accent', 'color' => '#1e73be', ), ); return $defaults; } );
Hope this helps!
February 17, 2022 at 2:08 am #2122335ElvisThanks Tom,
I have seen this function on the forum allready, what bugs me is, that then it prevents me to change the color values in the customizer without opening up code editor.
TnxFebruary 17, 2022 at 11:50 am #2123168TomLead DeveloperLead DeveloperThe code I shared above changes the defaults – it doesn’t overwrite the values you’ve set in the Customizer, and won’t prevent you from using the Customizer to change those values.
-
AuthorPosts
- You must be logged in to reply to this topic.