Plugin Widget Development – Conditional options

Asked on August 25, 2015 in Development.

I am developing a widget for layers. There are a couple of options that are dependent on the selection of other options, i.e. if option A is x then show option B.

My issue is within the form-item within an accordion section, I have a select form type and a text box – I want the text box to only display when a particular option is selected.

Here is the code I currently have:

<label for="get_custom_field_id( $widget_details, 'media_gallery_item', $media_gallery_guid, 'media_gallery_type' ); ?>">

form_elements()->input(

array(

'type' => 'select',

'name' => $this->get_custom_field_name( $widget_details, 'media_gallery_item', $media_gallery_guid, 'media_gallery_type' ),

'id' => $this->get_custom_field_id( $widget_details, 'media_gallery_item', $media_gallery_guid, 'media_gallery_type' ),

'placeholder' => __( 'Select a Media Type' , SKIZZAR__PLUGIN_SLUG ),

'value' => ( isset( $media_gallery_type ) ) ? $media_gallery_type : NULL ,

'class' => 'layers-select',

'options' => array(

'media_gallery_image' => __( 'Image' , SKIZZAR__PLUGIN_SLUG ),

'media_gallery_video' => __( 'Video' , SKIZZAR__PLUGIN_SLUG ),

'media_gallery_heading' => __( 'Heading' , SKIZZAR__PLUGIN_SLUG )

)

)

); ?>
<label for="get_custom_field_id( $widget_details, 'media_gallery_item', $media_gallery_guid, 'media_gallery_heading' ); ?>">

form_elements()->input(

array(

'type' => 'text',

'name' => $this->get_custom_field_name( $widget_details, 'media_gallery_item', $media_gallery_guid, 'media_gallery_type' ),

'id' => $this->get_custom_field_id( $widget_details, 'media_gallery_item', $media_gallery_guid, 'media_gallery_type' ),

'placeholder' => __( 'Heading' , SKIZZAR__PLUGIN_SLUG ),

'value' => ( isset( $media_gallery_heading ) ) ? $media_gallery_heading : NULL ,

'class' => 'layers-text',

'data' => array( 'show-if-selector' => '#' . $this->get_field_id( 'media_gallery_type' ), 'show-if-value' => 'media_gallery_heading' )

)

); ?>

This is the code that should show and hide the setting – however, it currently doesn’t work.
I also have this same issue with a checkbox – I have declared the checkbox at the start of my code – but still, no change when I toggle between the states.
Any ideas what I’m doing wrong?

Sam Skirrow Power User

Giving this question a bump – perhaps the plugin author can have a quick look at this question? I’ve bumped into this issue twice now where I need the ability to have conditional logic. i.e. when parameter 1 is set to A, parameter 2 is displayed.

on August 31, 2015.

Hi there,
Note that there is no way to “bump” a question here.  If you need dev help urgently, please consider joining our #slack chat, or give the question a go under the WordPress dev channel on stackexchange.

on September 2, 2015.

Plugin widget development – Conditional parameters are very important. Therefore, it is necessary to use revert, changing I had experience in this. I want to share with you one company. This is not an advertisement. Deployplace helped me deal with my problem.

on December 15, 2019.
Reply
  • Possible Answers to Your Question:

  • 1 Answer(s)
    Answered on September 2, 2015.

    This is really tricky to do in the customizer in general. To do it correctly, you would need javascript and ajax to handle the live selection.  To do it the most basic way, you can use a normal PHP condition

    Reply