Description

Piklist makes it easy to show one or more fields, based on the value of another field, and so much more. The Piklist conditions parameter is very powerful, and you can see all the options in the documentation.

In this tutorial, if the user clicks on the “Yes” radio button to subscribe to our newsletter, we will show an “Email” text field.

  1. Conditional fields work on almost every page in WordPress. In this tutorial we are going to build these fields for a Post.
  2. First let’s build a metabox using our base tutorial. You can stop at Step 4, since we will be creating our own fields.
  3. Now, let’s add two fields:
    1. The Control field will be a radio button
    2. The Conditional field will only display if “Yes” is selected in the radio button.

Build the Control Field

Building the Control field in Piklist is exactly the same as building any other field. There is absolutely nothing different about it. In this case, we are going to build the standard radio field that will ask the user if they want to subscribe to the newsletter:

1
2
3
4
5
6
7
8
9
10
11
12
13
piklist('field', array(
 'type' => 'radio'
 ,'field' => 'subscribe_to_newsletter'
 ,'label' => 'Would you like to subscribe to our newsletter?'
 ,'attributes' => array(
 'class' => 'text'
 )
 ,'choices' => array(
   'yes' => 'Yes'
   ,'no' => 'No'
 )
 ,'value' => 'no'
));

Note, that we set value => ‘no’. This sets the default value of the field to “No”, which hides the email box.

Build the Conditional Field

Let’s start with a standard Piklist text box.

1
2
3
4
5
6
7
piklist('field', array(
 'type' => 'text'
 ,'field' => 'email_address'
 ,'label' => 'Email Address'
 ,'description' => 'Please enter your email address'
 )
));

Now we are going to add the “conditions” array:

1
2
3
4
5
6
,'conditions' => array(
   array(
    'field' => 'subscribe_to_newsletter'
    ,'value' => 'yes'
   )
 )

This array can actually hold a variety of conditions. In this case, we are saying: If the field “subscribe_to_newsletter” (our Control field name) equals yes, then show this field.

All together the conditional field looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
piklist('field', array(
 'type' => 'text'
 ,'field' => 'email_address'
 ,'label' => 'Email Address'
 ,'description' => 'Please enter your email address'
 ,'conditions' => array(
     array(
      'field' => 'subscribe_to_newsletter'
      ,'value' => 'yes'
     )
   )
));

Full Code Snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
piklist('field', array(
 'type' => 'radio'
 ,'field' => 'subscribe_to_newsletter'
 ,'label' => 'Would you like to subscribe to our newsletter?'
 ,'attributes' => array(
 'class' => 'text'
 )
 ,'choices' => array(
   'yes' => 'Yes'
   ,'no' => 'No'
 )
 ,'value' => 'no'
));
 
piklist('field', array(
 'type' => 'text'
 ,'field' => 'email_address'
 ,'label' => 'Email Address'
 ,'description' => 'Please enter your email address'
 ,'conditions' => array(
   array(
    'field' => 'subscribe_to_newsletter'
    ,'value' => 'yes'
   )
 )
));