1

I'm very new with Redux. I don't know how to show and hide a textarea using switch (without saving). When my switch turns on, the text input displays and when it turns off, the input hides itself. I tried to use action hook but it didn't work.

Here's my code:

Redux::setSection( $opt_name, array(
    'title'     => __( 'Test', 'redux-test' ),
    'id'        => 'switch_buttonset',
    'desc'      => __( '', 'redux-test'),
    'icon'      => 'el el-cog',
    'fields'    => array(
        array(
            'id'       => 'opt-text-email',
            'type'     => 'text',
            'title'    => __( 'Title', 'redux-test' ),
        ),
        array(
            'id'       => 'switch-on',
            'type'     => 'switch',
            'title'    => __( 'Switch On', 'redux-framework-demo' ),
            'default'  => true,
        ),
    )
) );

add_action( 'redux/field/' . $opt_name . '/switch/callback/after', 'compiler_action', 10, 3 );
function compiler_action( $data ) {
    global $opt_name;
    Redux::hideField( $opt_name, 'opt-text-email' );
}
  • May be a typo but you're missing a ' single quote in your desc value. 'redux-test' – Howdy_McGee Jan 21 '16 at 16:10
  • Thank @Howdy_McGee for your quick answer, but it's not the problem. Sorry for my mistake in my question. – Trong Lam Phan Jan 21 '16 at 16:19
0

This is an old question but if you are still looking, what you want is the dependency option.

You can use it like this :

Redux::setSection( $opt_name, array(
    'title'  => __( 'Test', 'redux-test' ),
    'id'     => 'switch_buttonset',
    'desc'   => __( '', 'redux-test' ),
    'icon'   => 'el el-cog',
    'fields' => array(
        array(
            'id'      => 'switch-on',
            'type'    => 'switch',
            'title'   => __( 'Switch On', 'redux-framework-demo' ),
            'default' => true,
        ),
        array(
            'id'         => 'opt-text-email',
            'type'       => 'textarea',
            'title'      => __( 'Title', 'redux-test' ),
            'dependency' => array(
                'element' => 'switch-on', //Name of the field that will hide or show this one
                'value'   => array( 'true', 1 ) //The value it should have for it to be displayed
            ),
        )
    )
) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.