How to set required field by using the wp_dropdown_categories?

wp_dropdown_categories example
wp_dropdown_categories multiple select
wp_dropdown_categories custom taxonomy
wp_dropdown_categories exclude
wp_dropdown_categories link
wordpress filter posts by category dropdown
wp_dropdown_categories only parent
dropdown with category

After tried a lot I did not find any solution that works for me. I am using the below code to display the categories as a dropdown list.

<?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category' ); ?>

The dropdown display perfectly as expected but how can I make it required field? I tried by using jQuery with the following code but it works with some browser and sometimes it does not.

jQuery("#cat").attr("required", true);
jQuery("#cat option:first").val(null);

So is there a way that I can make the dropdown field required? I read the codex but did not find any parameter that will make it required field and even I never found an topic in online regarding this.


Try it the other way around, as required works only with dropdowns that have empty value, if you set it later, some browsers may not recognize it:

jQuery("#cat option:first").val(null);
jQuery("#cat").attr("required", true);

How to add HTML5 'required' attribute to wp_dropdown_categories , If you want to apply the required attribute every time you use you can use wp_dropdown_categories with echo argument set to false , introduce the If you want to add the "required"-Attribute just to a specific select-field, you  If you want to apply the required attribute only in especific situations, you can use wp_dropdown_categories with echo argument set to false, introduce the required attribute in the returned string and then echo: $args = array( 'show_option_none' => __( 'Select one', 'text-domain' ), 'taxonomy' => 'my_tax', 'id' => 'tax-type', 'echo' => false, ); $cat_dropdown = wp_dropdown_categories( $args ); $cat_dropdown = preg_replace( '^' . preg_quote( '<select ' ) . '^', '<select required ', $cat


Update: WordPress 4.6

As from WordPress 4.6 we can add a 'required' attribute directly to wp_dropdown_categories().

<?php
$dropdown_args = array(
    'show_option_none'   => __( 'Select Company', 'text-domain' ),
    'taxonomy'           => 'company',
    'required'           => true
);
wp_dropdown_categories( $dropdown_args );

Source: Core Tract Ticket

wp_dropdown_categories() | Function, WordPress lookup for wp_dropdown_categories, a WordPress Function. @​type string $option_none_value Value to use when no category is selected. Default Accepts any valid term field: 'term_id', 'name', 'slug', 'term_group', '​term_taxonomy_id', Default false (create select element even if no categories are found). By default, wp_dropdown_categories only returns categories that have been assigned to at least one post. To override this set the hide_empty parameter to false ("0"). <?php wp_dropdown_categories( 'hide_empty=0' ); ?>


You can modify the entire markup if you don't echo the result. echo is one of the function's arguments. So you could do something like:

$dropdown = wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category&echo=0' );

This then gives you access to the dropdown as a string, and you can modify that string as you see fit, before echoing it.

wp_dropdown_categories (WordPress Function), Here I show how to create a custom Walker Class, and use this as a custom The field was displayed using the generic wp_dropdown_categories() setting is as an optgroup – then a bit of code tinkering was required. Displays or retrieves the HTML dropdown list of categories. WordPress lookup for wp_dropdown_categories, a WordPress Function.


WP_Dropdown_Categories And Parent Optgroups, When it is enabled it will use the value in the depth argument. Accepts any valid term field: term_id,, name., slug, term_group, term_taxonomy_id, taxonomy,​  I have a product search widget when you select certain products a hidden field is shown. I would like to set a custom category in my widget options that is then used for displaying my hidden field. I have the front end working 100% when I add the parent category_id manually to my code. Now I just need to set the category option in my back end.


wp_dropdown_categories | function | WordPress, orderby: (string) (optional) Column name to use for ordering the categories. Default: ''. exclude_tree: (string) (required) optional For example, if 'value_field' is set to "slug", then 'selected' will take a slug instead of an ID. Accepts any valid term field: 'term_id', 'name', 'slug', 'term_group', 'term_taxonomy_id', '​taxonomy',  Based on the default search form provided by WordPress, this form outputs a field for the search query, a select box containing all of your Categories, and a button to trigger the search. Note the arguments passed to wp_dropdown_categories() that define the ‘Any Category’ label and set the name of the dropdown as swp_category_limiter.


Function Reference/wp dropdown categories, Build your own search form with a Categories dropdown; Customize the provided by WordPress, this form outputs a field for the search query, a select box containing all of your Categories, and a button to trigger the search. Note the arguments passed to wp_dropdown_categories() that define the No coding required! Now, WordPress already provides a ‘select‘ control which displays a dropdown, and all you need to use it is an associative array of key-value pairs. Generating such an array for (hierarchical) categories is very involved however, as is evident from inspecting the source of the wp_dropdown_categories() WordPress function.