[Resolved] Changing global colors programatically

Home Forums Support [Resolved] Changing global colors programatically

Home Forums Support Changing global colors programatically

Viewing 15 posts - 16 through 30 (of 30 total)
  • Author
    Posts
  • #2123351
    Elvis

    Thanks, then it is exactly what I need.
    Still, when I click Default, it reverts to original names.

    here is the snippet, it is an /inc/colors.php included in functions.php cache is cleared.

    // Set Default colors
    function apuri_set_default_colors( $defaults ) {
    	$defaults['global_colors'] = array(
    			array(
    					'name' => __( 'Primary Dark', 'generatepress' ),
    					'slug' => 'primary-dark',
    					'color' => '#222222',
    			),
    			array(
    					/* translators: Contrast number */
    					'name' => sprintf( __( 'Primary Light', 'generatepress' ) ),
    					'slug' => 'primary-light',
    					'color' => '#ffffff',
    			),
    			array(
    					/* translators: Contrast number */
    					'name' => sprintf( __( 'Secondary Dark', 'generatepress' ) ),
    					'slug' => 'secondary-dark',
    					'color' => '#a6a6a6',
    			),
    			array(
    					'name' => __( 'Secondary Light', 'generatepress' ),
    					'slug' => 'secondary-light',
    					'color' => '#eeeeee',
    			),
    			array(
    					'name' => __( 'Accent', 'generatepress' ),
    					'slug' => 'accent',
    					'color' => '#ff5e5e',
    			),
    	);
    
    	return $defaults;
    }
    
    add_filter( 'generate_option_defaults', 'apuri_set_default_colors' );

    Thanks

    #2124898
    Tom
    Lead Developer
    Lead Developer

    Ah, is this when trying to revert the individual options to their defaults?

    If so, you need to do this:

    add_filter( 'generate_color_option_defaults', function( $defaults ) {
        $defaults['color_name'] = 'new-default';
        $defaults['another_color_name'] = 'another-new-default';
    
        return $defaults;
    } );

    You can find the color names and current defaults here: https://github.com/tomusborne/generatepress/blob/3.1.3/inc/defaults.php#L123-L190

    #2125498
    Elvis

    YES, that is it.
    I want to set the defaults to my values and names, but not loose any of customizer functionality. πŸ™‚
    Thanks so much.

    #2125885
    Tom
    Lead Developer
    Lead Developer

    You’re welcome πŸ™‚

    #2127867
    Elvis

    Hello,
    here I come again πŸ™‚

    It does not work still. This is in my /inc/global_colors.php file required from functions.php My colors are called like I want and they have values I want, but when open, Typography, Body, Text Color, that has been set to my ‘primary-dark’, if I click to the button Default, it reverts to ‘contrast’.

    /**
     * Set new colors, and color names
     * @link https://generatepress.com/forums/topic/changing-global-colors-programatically/#post-2122087
     */
    function apuri_set_default_colors( $defaults ) {
    	$defaults['global_colors'] = array(
    			array(
    					'name' => __( 'Primary Dark', 'apuri' ),
    					'slug' => 'primary-dark',
    					'color' => '#222222',
    			),
    			array(
    					'name' => sprintf( __( 'Primary Light', 'apuri' ) ),
    					'slug' => 'primary-light',
    					'color' => '#ffffff',
    			),
    			array(
    					'name' => sprintf( __( 'Secondary Dark', 'apuri' ) ),
    					'slug' => 'secondary-dark',
    					'color' => '#a6a6a6',
    			),
    			array(
    					'name' => __( 'Secondary Light', 'apuri' ),
    					'slug' => 'secondary-light',
    					'color' => '#eeeeee',
    			),
    			array(
    					'name' => __( 'Accent', 'apuri' ),
    					'slug' => 'accent',
    					'color' => '#ff5e5e',
    			),
    	);
    
    	return $defaults;
    }
    
    add_filter( 'generate_option_defaults', 'apuri_set_default_colors' );
    
    /**
     * Set new defaults, to use in customizer
     * @link https://generatepress.com/forums/topic/changing-global-colors-programatically/page/2/#post-2124898
     */
    
    function apuri_reset_new_color_defaults_for_customizer( $defaults ) {
    	$defaults['contrast']     = 'primary-dark';
    	$defaults['base-2']       = 'primary-light';
    	$defaults['contrast-2']   = 'secondary-dark';
    	$defaults['base-3']       = 'secondary-light';
    	$defaults['accent']       = 'accent';
    
    	return $defaults;
    }
    
    add_filter( 'generate_color_option_defaults', 'apuri_reset_new_color_defaults_for_customizer');
    #2128336
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Those aren’t the correct option names.

    For example, the option name on the following line is top_bar_background_color: https://github.com/tomusborne/generatepress/blob/3.1.3/inc/defaults.php#L123

    So you would do this: $defaults['top_bar_background_color'] = 'primary-dark';

    Hope this helps!

    #2128716
    Elvis

    [edited]
    Tom Thanks a lot,
    I got it solved!

    This syntax works.

    $defaults['navigation_search_background_color']     = 'var(--primary-light)';
    $defaults['navigation_search_text_color']           = 'var(--secondary-dark)';
    $defaults['content_background_color']               = 'var(--primary-light)';
    $defaults['content_text_color']                     = 'var(--primary-dark)';

    Woohooo!

    #2129937
    Tom
    Lead Developer
    Lead Developer

    Glad you got it sorted πŸ™‚

    #2602040
    Steve

    Hi Tom,

    Is your code you posted back on the previous page (#2122087) still valid?

    I have copied and pasted that into WPCodebox but it doesn’t seem to be having any effect – I’m making the accent black for example just to see if it’s working but there appears to be no change to the global colours in the customiser at all (accent remains blue)

    Screenshot-2023-04-08-at-14-49-32

    #2602394
    Ying
    Staff
    Customer Support

    Hi there,

    The code is still valid.

    It only changes the default color which is #1e73be for Accent, if you’ve already changed the color, then the code would not have any effect.

    #2602401
    Steve

    hmmm strange – I wonder why it isn’t working then?

    I had not changed the default accent colour – it is still showing as #1e73be in the customiser.

    #2602413
    Ying
    Staff
    Customer Support

    In that case, this code should still work.

    I haven’t tried WPCodebox plugin, can you try using Code snippet to add the code?

    #2602429
    Steve

    I tried in Code Snippets and no luck with that either…

    Screenshot-2023-04-08-at-20-40-18

    Screenshot-2023-04-08-at-20-40-42

    #2603178
    Steve

    I just don’t understand why the generate_option_defaults filter isn’t working.

    I have lots of other snippets working in WPCodebox

    Also this color settings code which I found elsewhere on the forum works fine – although this permanently sets the colours, not just setting default values:

    <?php 
    
    add_filter( 'option_generate_settings', function( $settings ) {
        $settings['global_colors'] = [
            [
                'name' => __( 'contrast', 'generatepress' ),
                'slug' => 'contrast',
                'color' => '#8cff44',
            ],
            [
                'name' => sprintf( __( 'Contrast %s', 'generatepress' ), '2' ),
                'slug' => 'contrast-2',
                'color' => '#29cc14',
            ],
            [
                'name' => sprintf( __( 'Contrast %s', 'generatepress' ), '3' ),
                'slug' => 'contrast-3',
                'color' => '#c2cec7',
            ],
            [
                'name' => __( 'base', 'generatepress' ),
                'slug' => 'base',
                'color' => '#f0f0f0',
            ],
            [
                'name' => sprintf( __( 'Base %s', 'generatepress' ), '2' ),
                'slug' => 'base-2',
                'color' => '#f0f7ff',
            ],
            [
                'name' => sprintf( __( 'Base %s', 'generatepress' ), '3' ),
                'slug' => 'base-3',
                'color' => '#ffffff',
            ],
            [
                'name' => __( 'accent', 'generatepress' ),
                'slug' => 'accent',
                'color' => '#f1276a',
            ],
        ];    
    
        return $settings;
    } );
    #2603240
    Ying
    Staff
    Customer Support

    I would recommend disabling all your plugins except GP premium and the plugin inserting PHP code to test.

    Also, disable all your other custom functions to eliminate conflicts.

    If you are using a child theme, switch to the parent theme.

Viewing 15 posts - 16 through 30 (of 30 total)
  • You must be logged in to reply to this topic.