Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Active Callback not Working #1620

Closed
JakeHenshall opened this issue Nov 9, 2017 · 7 comments
Closed

Active Callback not Working #1620

JakeHenshall opened this issue Nov 9, 2017 · 7 comments
Milestone

Comments

@JakeHenshall
Copy link

JakeHenshall commented Nov 9, 2017

Issue description:

The Toggle and switch do not take the 'default' value into consideration. If I set it to 1 it's disabled if I set the 0 it's also disabled it doesn't matter which one. if the Switch/toggle has an active callback it doesn't display.

Version used:

(Did you try using the develop branch from github? There's a chance your issue has already been addressed there)
I'm currently embedding into the theme, I've tried the lastest and dev branches

Using theme_mods or options?

theme_mods

Code to reproduce the issue (config + field(s))

Kirki::add_field( 'malinois', array(
	'type'        => 'color',
	'settings'    => 'body_border_color',
	'label'       => __( 'Body Border Colour', 'my_textdomain' ),
	'section'     => 'settings',
	'default'     => '#000000',
	'priority'    => 7,
	'choices'     => array(
		'alpha' => true,
	),
) );

Kirki::add_field( 'malinois', array(
	'type'        => 'switch',
	'settings'    => 'body_border',
	'label'       => __( 'Body Border (Passepartout)', 'my_textdomain' ),
	'section'     => 'settings',
	'default'     => '1',
	'priority'    => 6,
	'choices'     => array(
		'on'  => esc_attr__( 'Enable', 'my_textdomain' ),
		'off' => esc_attr__( 'Disable', 'my_textdomain' ),
	),
) );

and

Kirki::add_field( 'malinois', array(
	'type'        => 'color',
	'settings'    => 'body_border_color',
	'label'       => __( 'Body Border Colour', 'my_textdomain' ),
	'section'     => 'settings',
	'default'     => '#000000',
	'priority'    => 7,
	'choices'     => array(
		'alpha' => true,
	),
) );

Kirki::add_field( 'malinois', array(
	'type'        => 'switch',
	'settings'    => 'body_border',
	'label'       => __( 'Body Border (Passepartout)', 'my_textdomain' ),
	'section'     => 'settings',
	'default'     => '1',
	'priority'    => 6,
	'choices'     => array(
		'on'  => esc_attr__( 'Enable', 'my_textdomain' ),
		'off' => esc_attr__( 'Disable', 'my_textdomain' ),
	),
    'active_callback'    => array(
    	array(
    		'setting'  => 'body_border_color',
    		'operator' => '==',
    		'value'    => '1',
    	),
    ),
) );

Hopefully ive got the active_callback right.

@aristath
Copy link
Contributor

aristath commented Nov 9, 2017

Can you please try using a boolean value for your defaults?
So true or false instead of '1' or '0'

@JakeHenshall
Copy link
Author

JakeHenshall commented Nov 9, 2017

@aristath Sure ill try that thanks, ill let you know if this works, but 1 / 0 is in the docs. So the docs might need updating? https://aristath.github.io/kirki/docs/controls/switch.html

@aristath
Copy link
Contributor

aristath commented Nov 9, 2017

Yes, the docs are definitely wrong.... Docs for some controls (including toggle/switch) have not been updated in 2 years
I have started slowly improving them these days but it's an ongoing process, it's going to take me some time.

@JakeHenshall
Copy link
Author

JakeHenshall commented Nov 9, 2017

@aristath I've just tried this and unfortunately this hasn't worked. :(

Kirki::add_field( 'malinois', array(
	'type'        => 'color',
	'settings'    => 'body_border_color',
	'label'       => __( 'Body Border Colour', 'malinois' ),
	'section'     => 'settings',
	'default'     => '#000000',
	'priority'    => 7,
	'choices'     => array(
		'alpha' => true,
	),
) );

Kirki::add_field( 'malinois', array(
	'type'        => 'switch',
	'settings'    => 'body_border',
	'label'       => __( 'Body Border (Passepartout)', 'malinois' ),
	'section'     => 'settings',
	'default'     => false,
	'priority'    => 6,
	'choices'     => array(
		'on'  => esc_attr__( 'Enable', 'malinois' ),
		'off' => esc_attr__( 'Disable', 'malinois' ),
	),
    'active_callback'    => array(
    	array(
    		'setting'  => 'body_border_color',
    		'operator' => '==',
    		'value'    => true,
    	),
    ),
) );

screen shot 2017-11-09 at 18 33 18

It still shows the body border colour even when disabled?

Thanks Jake.

@aristath
Copy link
Contributor

Closed this one by mistake, reopening.

@aristath aristath reopened this Nov 11, 2017
@aristath aristath added this to the 3.0.14 milestone Nov 11, 2017
@aristath
Copy link
Contributor

@JakeHenshall I'm sorry but I just noticed this... It was so obvious that I missed it.

You have a switch and a color control.
You want to show the color control when the switch is on.

The way this works is the reverse of what you have on your code.

Kirki::add_field( 'malinois', array(
	'type'        => 'color',
	'settings'    => 'body_border_color',
	'label'       => __( 'Body Border Colour', 'my_textdomain' ),
	'section'     => 'settings',
	'default'     => '#000000',
	'priority'    => 7,
	'choices'     => array(
		'alpha' => true,
	),
	'active_callback'    => array(
		array(
			'setting'  => 'body_border',
			'operator' => '==',
			'value'    => 1,
		),
	),
) );

Kirki::add_field( 'malinois', array(
	'type'        => 'switch',
	'settings'    => 'body_border',
	'label'       => __( 'Body Border (Passepartout)', 'my_textdomain' ),
	'section'     => 'settings',
	'default'     => '1',
	'priority'    => 6,
	'choices'     => array(
		'on'  => esc_attr__( 'Enable', 'my_textdomain' ),
		'off' => esc_attr__( 'Disable', 'my_textdomain' ),
	),
) );

You don't tell the switch to turn on or off other controls. Instead you have to tell the other controls to watch the switch's value so that they may be show or hidden based on the switch's value. I don't know if that sentence makes a lot of sense, I hope it does 😄
See the active_callback is a set of conditions that apply to the control and decide if the control will be shown or not.

Tested the code I just posted and it works fine so I'll go ahead and close this ticket 👍

@JakeHenshall
Copy link
Author

@aristath Ahh awesome, works perfectly, thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants