Skip to content

Instantly share code, notes, and snippets.

@dovy
Last active March 4, 2021 14:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dovy/6478863 to your computer and use it in GitHub Desktop.
Save dovy/6478863 to your computer and use it in GitHub Desktop.
Example of the required attribute for the Redux Framework.
<?php
$options = array(
'required' => array('id','equals',array( 1,3 ) ) // Multiple values
);
$options = array(
'required' => array('id','equals', array( 1 ) ) // Single value
);
$options = array(
'required' => array('id','equals', 1 ) // Also a single value
);
$options = array(
'required' => array('id','equals', false ) // Means id = 0 or false or in the case of a switch, off
);
<?php
$options[] =
array(
'id'=>'switch-custom',
'type' => 'switch',
'title' => __('Switch - Custom Titles', 'redux-framework'),
'subtitle'=> __('Look, it\'s on! Also hidden child elements!', 'redux-framework'),
"default" => 0,
'on' => 'Enabled',
'off' => 'Disabled',
),
array(
'id'=>'switch-required',
'type' => 'switch',
'required' => array('switch-custom', '=' , '1')
'title' => __('Switch - With Hidden Items (NESTED!)', 'redux-framework'),
'subtitle'=> __('Also called a "required" parent.', 'redux-framework'),
'desc' => __('Items set with a required to this ID will hide unless this is set to the appropriate value.', 'redux-framework'),
'default' => 0,
),
array(
'id'=>'patterns',
'type' => 'image_select',
'tiles' => true,
'required' => array('switch-custom', '=' , '0')
'title' => __('Images Option (with pattern=>true)', 'redux-framework'),
'subtitle'=> __('Select a background pattern.', 'redux-framework'),
'default' => 0,
'options' => array(
'1' => array('alt' => '1 Column', 'img' => REDUX_URL.'assets/img/1col.png'),
'2' => array('alt' => '2 Column Left', 'img' => REDUX_URL.'assets/img/2cl.png'),
'3' => array('alt' => '2 Column Right', 'img' => REDUX_URL.'assets/img/2cr.png'),
'4' => array('alt' => '3 Column Middle', 'img' => REDUX_URL.'assets/img/3cm.png'),
'5' => array('alt' => '3 Column Left', 'img' => REDUX_URL.'assets/img/3cl.png'),
'6' => array('alt' => '3 Column Right', 'img' => REDUX_URL.'assets/img/3cr.png')
),
)
);
  • = / equals

    • Exact value of parent.
  • != / not

    • Value of parent is not equal to this.
  • > / greater / is_larger

    • Value is larger than the value of the parent. Must be an int/float value. No string comparisons will function.
  • < / less / is_smaller

    • Value is less than the value of the parent. Must be an int/float value. No string comparisons will function.
  • contains

    • String comparison. Checks to see if value is within the parent's value.
  • doesnt_contains

    • String comparison. Checks to see if value is not within the parent's value.
  • is_empty_or

    • Checks to see if the value of the parent is empty or equal to the current value. Same as equals, but also allows visibility if parent value is not set.
  • not_empty_and

    • Checks to see if the value of the parent is not empty and equal to the current value. Same as not, but also hides visibility if parent value is not set.
@peteroravec
Copy link

Can you update this GIST to 3.2.0 version?
because required with multiple values no longer working
'required' => array('id','equals',array( 1,3 ) )

@emcniece
Copy link

Bump for update. How do we make the required field correspond to multiple values now?

@emcniece
Copy link

Got around this by using multiple "not" requirements. Since you can array-nest multiple conditions and the field only appears if all conditions are met, by using the "not" condition we essentially create a NAND instead of an OR. For a select option with 4 settings, you can get your option to show for settings 2 and 4 like so:

array(
    'id' => 'section-left-sidebar-start',
    'type' => 'section',
    'title' => __('Left Sidebar', 'redux-framework-demo'),
    'subtitle' => __('Adjust settings for the left sidebar region.', 'redux-framework-demo'),
    'indent' => true,
    'required' => array(
        array('opt-layout', 'not', 1),
        array('opt-layout', 'not', 3),
    ),                        
),

@mstoic
Copy link

mstoic commented Dec 30, 2014

Is there a way to use required with section? I mean, display sections on condition?

@mstoic
Copy link

mstoic commented Mar 20, 2015

Any way to change default values on condition?

@cyberwani
Copy link

cyberwani commented Feb 15, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment