Kirki: Active Callback

Created on 29 Mar 2017  ·  7Comments  ·  Source: kirki-framework/kirki

Issue description:

How do i combine two different fields in active_callback with OR Logic?

I want to show "my_textarea_1" field in following condition

my_switch_1 field value 1 or my_select_1 field value options-1 or option-2

Kirki::add_field( 'my_config', array(

'type'     => 'switch',
'settings' => 'my_switch_1',
'label'    => __( 'Switch 1', 'my_textdomain' ),
'section'  => 'dt_section_1',
'default'  => 0,
'choices'  => array(
    'on'  => esc_attr__( 'Yes', 'my_textdomain' ),
    'off' => esc_attr__( 'No', 'my_textdomain' )
),
));

Kirki::add_field( 'my_config', array( 'type' => 'select',
'settings' => 'my_select_1',
'label' => __( 'Select 1', 'my_textdomain' ),
'section' => 'dt_section_1',
'default'     => 'option-1',
'multiple'    => 1,
'choices'     => array(
    'option-1' => esc_attr__( 'Option 1', 'my_textdomain' ),
    'option-2' => esc_attr__( 'Option 2', 'my_textdomain' ),
    'option-3' => esc_attr__( 'Option 3', 'my_textdomain' ),
    'option-4' => esc_attr__( 'Option 4', 'my_textdomain' ),
)
 ) );

Kirki::add_field( 'my_config', array(
'type'     => 'textarea',
'settings' => 'my_textarea_1',
'label'    => __( 'Textarea 1', 'my_textdomain' ),
'section'  => 'dt_section_1',
'active_callback' => array(
    /* I NEED HELP HERE */
    array( 'setting' => 'my_switch_1', 'operator' => '==', 'value' =>  '1' ),   
    array( 'setting' => 'my_select_1', 'operator' => 'in', 'value' =>  array( 'option-1', 'option-2' ) )
)
));

Most helpful comment

For reference, this is the full code I used on my test site and worked fine:

Kirki::add_config( 'my_config' );
Kirki::add_section( 'dt_section_1', array(
    'title' => 'Section test',
));

Kirki::add_field( 'my_config', array(
    'type'     => 'switch',
    'settings' => 'my_switch_1',
    'label'    => __( 'Switch 1', 'my_textdomain' ),
    'section'  => 'dt_section_1',
    'default'  => 0,
    'choices'  => array(
        'on'  => esc_attr__( 'Yes', 'my_textdomain' ),
        'off' => esc_attr__( 'No', 'my_textdomain' )
    ),
));

Kirki::add_field( 'my_config', array(
    'type'     => 'select',
    'settings' => 'my_select_1',
    'label'    => __( 'Select 1', 'my_textdomain' ),
    'section'  => 'dt_section_1',
    'default'  => 'option-1',
    'multiple' => 1,
    'choices'  => array(
        'option-1' => esc_attr__( 'Option 1', 'my_textdomain' ),
        'option-2' => esc_attr__( 'Option 2', 'my_textdomain' ),
        'option-3' => esc_attr__( 'Option 3', 'my_textdomain' ),
        'option-4' => esc_attr__( 'Option 4', 'my_textdomain' ),
    )
) );

Kirki::add_field( 'my_config', array(
    'type'            => 'textarea',
    'settings'        => 'my_textarea_1',
    'label'           => __( 'Textarea 1', 'my_textdomain' ),
    'section'         => 'dt_section_1',
    'active_callback' => array(
        array(
            array(
                'setting'  => 'my_switch_1',
                'operator' => '==',
                'value'    =>  '1'
            ),
            array(
                'setting'  => 'my_select_1',
                'operator' => 'in',
                'value'    =>  array( 'option-1', 'option-2' ),
            ),
        ),
    ),
));

All 7 comments

Hi

Try this:

array( 'setting' => 'my_switch_1', 'operator' => '==', 'value' =>  'on' ),  
array( 'setting' => 'my_select_1', 'operator' => 'contains', 'value' =>  array( 'option-1', 'option-2' ) )

@x-bull No Luck

'array( 'setting' => 'my_switch_1', 'operator' => '==', 'value' => 'on' )
value should be 1 or 0 only

'active_callback' => array(
    array( 
        'setting' => 'my_switch_1', 
        'operator' => '==', 
        'value' =>  '1' 
    ),  
    array( 
        'setting' => 'my_select_1', 
        'operator' => '==', 
        'value' =>  'option-1', 
    ),
    array(
        'setting' => 'my_select_1',
        'operator' => '==',
        'value' => 'option-2' 
    ),
),

OK, I think I see the issue.

What you want is this:

my_switch_1 field value 1 or my_select_1 field value options-1 or option-2

The key to the above is the OR you want.

active_callback works like this:

active_callback => array( condition_1 AND condition_2 AND condition_3 )

Each condition can be like this:
condition => array( setting, operator, value )
OR it can be like this:
condition => array( subcondition_1 OR subcondition_2 OR subcondition_3 )

This code:

    'active_callback' => array(
        array(
            'setting'  => 'my_switch_1',
            'operator' => '==',
            'value'    =>  '1'
        ),
        array(
            'setting'  => 'my_select_1',
            'operator' => 'in',
            'value'    =>  array( 'option-1', 'option-2' ),
        ),
    ),

Can be translated like this:

If the value of my_switch_1 is 1, AND value of my_select_1 is one of option-1/option-2.

What you need is this:

    'active_callback' => array(
        array(
            array(
                'setting'  => 'my_switch_1',
                'operator' => '==',
                'value'    =>  '1'
            ),
            array(
                'setting'  => 'my_select_1',
                'operator' => 'in',
                'value'    =>  array( 'option-1', 'option-2' ),
            ),
        ),
    ),

By nesting the conditions one level deeper, it's not AND but OR.

For reference, this is the full code I used on my test site and worked fine:

Kirki::add_config( 'my_config' );
Kirki::add_section( 'dt_section_1', array(
    'title' => 'Section test',
));

Kirki::add_field( 'my_config', array(
    'type'     => 'switch',
    'settings' => 'my_switch_1',
    'label'    => __( 'Switch 1', 'my_textdomain' ),
    'section'  => 'dt_section_1',
    'default'  => 0,
    'choices'  => array(
        'on'  => esc_attr__( 'Yes', 'my_textdomain' ),
        'off' => esc_attr__( 'No', 'my_textdomain' )
    ),
));

Kirki::add_field( 'my_config', array(
    'type'     => 'select',
    'settings' => 'my_select_1',
    'label'    => __( 'Select 1', 'my_textdomain' ),
    'section'  => 'dt_section_1',
    'default'  => 'option-1',
    'multiple' => 1,
    'choices'  => array(
        'option-1' => esc_attr__( 'Option 1', 'my_textdomain' ),
        'option-2' => esc_attr__( 'Option 2', 'my_textdomain' ),
        'option-3' => esc_attr__( 'Option 3', 'my_textdomain' ),
        'option-4' => esc_attr__( 'Option 4', 'my_textdomain' ),
    )
) );

Kirki::add_field( 'my_config', array(
    'type'            => 'textarea',
    'settings'        => 'my_textarea_1',
    'label'           => __( 'Textarea 1', 'my_textdomain' ),
    'section'         => 'dt_section_1',
    'active_callback' => array(
        array(
            array(
                'setting'  => 'my_switch_1',
                'operator' => '==',
                'value'    =>  '1'
            ),
            array(
                'setting'  => 'my_select_1',
                'operator' => 'in',
                'value'    =>  array( 'option-1', 'option-2' ),
            ),
        ),
    ),
));

@aristath., Thanks for your great solution. But if there were any kind of documentation on this topic, you'll no longer asked for such queries from the people at any time.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

runnickrun picture runnickrun  ·  3Comments

pagelab picture pagelab  ·  3Comments

fakrulislam picture fakrulislam  ·  3Comments

fastmarketo picture fastmarketo  ·  4Comments

stylethemes picture stylethemes  ·  6Comments