kirki icon indicating copy to clipboard operation
kirki copied to clipboard

'active_callback' option

Open SimoneM90 opened this issue 11 years ago • 17 comments

Can someone explain how to use the 'active_callback' option in a kirki field?

SimoneM90 avatar May 21 '15 07:05 SimoneM90

The active_callback argument can be used to determine when a field should be shown or hidden. There are 2 ways to use it:

Option 1: Write your own custom function:

function my_custom_callback() {
    return ( is_singular( 'recipe' ) ) ? true : false;
}

The above function for example will return true when you're viewing a single post that has post=type = 'recipe'. and it will return false when for example you're seeing an archive or a page. In this case, you can then call that function in your field like this:

$fields[] = array(
    'type'        => 'radio',
    'setting'     => 'radio_demo',
    'label'       => __( 'This is the label', 'kirki' ),
    'description' => __( 'This is the control description', 'kirki' ),
    'section'     => 'radio_section',
    'default'     => 'option-1',
    'priority'    => 10,
    'choices'     => array(
        'option-1' => __( 'Option 1', 'kirki' ),
        'option-2' => __( 'Option 2', 'kirki' ),
        'option-3' => __( 'Option 3', 'kirki' ),
        'option-4' => __( 'Option 4', 'kirki' ),
    ),
    'active_callback' => 'my_custom_callback',
);

This will cause your field to only be displayed when you're seeing a "recipe" in the customizer.

Option 2: If you want your field to be shown depending on the values of other fields in the customizer, then you can use the required argument. What we've done is wrote a function that parses your arguments and builds a query based on those. Example:

$fields[] = array(
    'type'        => 'radio',
    'setting'     => 'radio_demo',
    'label'       => __( 'This is the label', 'kirki' ),
    'description' => __( 'This is the control description', 'kirki' ),
    'section'     => 'radio_section',
    'default'     => 'option-1',
    'priority'    => 10,
    'choices'     => array(
        'option-1' => __( 'Option 1', 'kirki' ),
        'option-2' => __( 'Option 2', 'kirki' ),
        'option-3' => __( 'Option 3', 'kirki' ),
        'option-4' => __( 'Option 4', 'kirki' ),
    ),
    'required' => array(
        array(
            'setting'  => 'my_setting',
            'value'    => 'some-value',
            'operator' => '=='
        ),
        array(
            'setting'  => 'another_setting',
            'value'    => 'some-other-value',
            'operator' => '!='
        ),
    )
);

The above will make the field dependent on 2 other fields: my_setting and another_setting and it will make the field be displayed only when my_setting has a value of some-value and another_setting has a value not equal to some-other-value.

Please note that the above have not yet been included in the stable version available on w.org and you'd need the version from the github repo in order for them to work.

aristath avatar May 21 '15 13:05 aristath

Please note that you can't use both the active_callback AND required at the same time. If you use active_callback then it will override any required arguments you set.

aristath avatar May 21 '15 13:05 aristath

Then if I have a field like this:

$fields[] = array(
    'type'        => 'radio',
    'setting'     => 'radio_demo',
    'label'       => __( 'This is the label', 'kirki' ),
    'description' => __( 'This is the control description', 'kirki' ),
    'section'     => 'radio_section',
    'default'     => 'option-1',
    'priority'    => 10,
    'choices'     => array(
        'option-1' => __( 'Option 1', 'kirki' ),
        'option-2' => __( 'Option 2', 'kirki' ),
        'option-3' => __( 'Option 3', 'kirki' ),
        'option-4' => __( 'Option 4', 'kirki' ),
    ),
);

and I want to show:

$fields[] = array(
    'type'        => 'text',
    'setting'     => 'text_demo',
    'label'       => __( 'This is the label', 'kirki' ),
    'description' => __( 'This is the control description', 'kirki' ),
    'help'        => __( 'This is some extra help.', 'kirki' ),
    'section'     => 'text_section',
    'default'     => 'This is some default text',
    'priority'    => 10,
);

if radio_demo == 'option-1' the function is:

function my_custom_callback() {
   if  ( kirki_get_option( 'radio_demo' ) == 'options-1' )  {
return true;
} else {
return false;
}

and set 'active_callback' => 'my_custom_callback' option in text_demo field, right?

If i set the 'active_callback' option appears this message: :call_user_func() expects parameter 1 to be a valid callback, function active_callback not found or invalid function name in on line

Thanks for the help.

SimoneM90 avatar May 21 '15 22:05 SimoneM90

You can simply do this:

$fields[] = array(
    'type'        => 'text',
    'settings'    => 'text_demo',
    'label'       => __( 'This is the label', 'kirki' ),
    'description' => __( 'This is the control description', 'kirki' ),
    'help'        => __( 'This is some extra help.', 'kirki' ),
    'section'     => 'text_section',
    'default'     => 'This is some default text',
    'priority'    => 10,
    'required'    => array(
        array(
            'setting'  => 'radio_demo',
            'value'    => 'option-1',
            'operator' => '==',
        ),
    ),
);

no need for callback, as I mentioned earlier we already have a custom function that automatically builds it from the required argument.

aristath avatar May 21 '15 22:05 aristath

@aristath Sorry to post here like this... :( The required function doesnt work with 'options_type' => 'option', and when i tried to use the active_callback instead, I get this: :call_user_func() expects parameter 1 to be a valid callback, function active_callback not found or invalid function name in on line

towfiqi avatar May 22 '15 23:05 towfiqi

Confirmed. I'm marking this one as a bug and I'll work on it tomorrow. :)

aristath avatar May 23 '15 02:05 aristath

Try replacing this file: https://github.com/reduxframework/kirki/blob/6bb5e7558454bdb53e54503b44e30aed575f0c82/includes/helpers.php with this one: https://gist.github.com/aristath/cbdf0c9e16bfc94f6d95 and let me know if that works.

aristath avatar May 23 '15 02:05 aristath

thanks for the quick response ari :) Replaced the the code with no joy :/

towfiqi avatar May 23 '15 07:05 towfiqi

@towfiqi Can you please test if this works for you now? If you can't get it to work, can you test if this implementation works for you https://github.com/reduxframework/kirki/wiki/required ? We're adding a new API so if you're just starting now I'd advise you to use the new methods described there. Future versions will be using that instead of the filters. If it does work with that implementation then I'll need to add some logic to back-port it to the previously used methods using the kirki/fields filter.

aristath avatar May 24 '15 13:05 aristath

@aristath thanks for the response :) I just downloaded the latest version of kirki from github and tested again. doesnt seem to work.

and if I follow the new method, how would my config file look? any example? With what do I replace the 'dummy_config' in this line: Kirki::add_config( 'dummy_config' );

Let me know

towfiqi avatar May 24 '15 19:05 towfiqi

If you don't want to use the config then you can just skip it and add your fields like this:

Kirki::add_field( '', array() );

aristath avatar May 25 '15 19:05 aristath

Hello @aristath,

Same here. I got problem with the required argument.

Here is my code.

$fields[] = array( 'type' => 'radio', 'settings' => 'htypes', 'label' => __( '', 'kirki' ), 'description' => __( '', 'kirki' ), 'section' => 'header_types', 'priority' => 10, 'choices' => array( 'widget' => __( 'Widgetized', 'kirki' ), 'predef' => __( 'Predefined', 'kirki' ), ), );

$fields[] = array(
    'type'        => 'radio-image',
    'settings'    => 'headtype',
    'label'       => __( '', 'kirki' ),
    'description' => __( '', 'kirki' ),
    'section'     => 'header_types',
    'priority'    => 20,
    'default'    => 'htypes1',
    'choices'     => array(
        'htypes1' => __( 'First Option', 'kirki' ),
        'htypes2' => __( 'Second Option', 'kirki' ),
    ),
    'required'  => array(
        array(
            'setting'  => 'htypes',
            'value'    => 'predef',
            'operator' => '==',
        ),
    )
);

When you have sometime, please do let me know whats wrong. Why it isn't working and whats the work around?

Thanks much!

randomideas avatar Jun 10 '15 20:06 randomideas

@towfiqi did you get this one to work?

@randomideas I see you've already created a new ticket for your issue, I have already replied on https://github.com/reduxframework/kirki/issues/237#issuecomment-111719366

aristath avatar Jun 13 '15 15:06 aristath

@towfiqi any news on this one?

aristath avatar Jun 15 '15 18:06 aristath

Thank you @aristath appreciate your help! You da man!

randomideas avatar Jun 15 '15 18:06 randomideas

This has been fixed.

aristath avatar Jun 20 '15 15:06 aristath

Adding myself to this issue so I may follow it in the morning and give it another go. Thanks for the description of the required.

Active callback looks interesting.

ghost avatar Jun 20 '15 15:06 ghost