Reputation: 3064
I am using wp_dropdown_categories
in one of my custom forms. I want to add an additional item at top of the list with an empty
string so that it renders like:
<select id="cmd" name="cmb" required>
<option value=''>Select your language</option>
<option value='2'>English</option>
<option value='3'>Spanish</option>
<option value='4'>Italian</option>
<option value='5'>German</option>
</select>
This will allow me to use the native required
attribute of HTML5
and the validation will fire automatically. I tried show_options_all
already but the value of for this option comes up 0
, which won't validate by required
attribute.
<?php $args = array(
'show_option_all' => 'Select one',
'show_option_none' => '',
'option_none_value' => '-1',
'orderby' => 'Name',
'order' => 'ASC',
'taxonomy' => 'category',
'hide_if_empty' => false,
'value_field' => 'term_id',
); ?>
<?php wp_dropdown_categories( $args ); ?>
Is there a pure WordPress way to accomplish this?
Upvotes: 3
Views: 1845
Reputation: 1470
Try using show_option_none
and option_none_value
. I am not sure what your current argument list is, but add these:
<?php $args = array(
'show_option_none' => 'Select your language',
'option_none_value' => '',
); ?>
Let me know if this solves your problem. This will create an option with the desired empty value (""), with your text (Select your language).
More info here: https://codex.wordpress.org/Function_Reference/wp_dropdown_categories
Upvotes: 5