1 min read

Create A Complete WordPress Custom Taxonomy (Categories) Dropdown

This code snippet generates a dropdown menu in an HTML form for selecting different breeds. Here’s a breakdown of what it does:

  1. <form> tag:
    • It defines a form with the ID “breeddropdown” and the class “breedslist”.
    • The action attribute specifies the URL where the form data will be sent upon submission. In this case, it uses the bloginfo('url') function to dynamically retrieve the site’s URL.
  2. PHP code within the form:
    • It initializes an array $args with various parameters for generating the dropdown menu.
    • Parameters like ‘show_option_all’, ‘hide_empty’, ‘echo’, ‘show_count’, ‘taxonomy’, ‘orderby’, and ‘order’ are used to customize the appearance and behavior of the dropdown.
    • Additional parameters ‘name’, ‘value_field’, ‘selected’, ‘class’, and ‘id’ are added to enhance the functionality and styling of the dropdown.
    • The wp_dropdown_categories() function generates the HTML for the dropdown menu based on the provided arguments ($args). This function is specifically used for generating dropdowns of WordPress categories or taxonomies.
  3. JavaScript manipulation:
    • The generated dropdown HTML is then manipulated to include an onchange attribute. This attribute triggers form submission whenever the selection in the dropdown changes. This ensures that the form is submitted automatically upon selecting a breed without needing to click a separate submit button.
  4. <noscript> tag:
    • This tag provides a fallback for users who have JavaScript disabled in their browsers. It includes an input of type “submit” with the value “View”, allowing users to submit the form manually if JavaScript is not available.

Overall, this code snippet creates a dynamic dropdown menu for selecting breeds, ensuring smooth user experience with automatic form submission upon selection, while also providing a fallback option for users without JavaScript support.

<form id="breeddropdown" class="breedslist" action="<?php bloginfo('url'); ?>" method="get">

<?php
$args = array(
    'show_option_all' => __( 'Chews A Breed', 'l-p' ),
    'hide_empty'      => 0,
    'echo'            => 0,
    'show_count'      => 0,
    'taxonomy'        => 'breeds',
    'orderby'         => 'name',  // Order by name
    'order'           => 'ASC',   // Ascending order

    // Add these three args:
    'name'            => 'breeds', // same as the "taxonomy" above
    'value_field'     => 'slug',  // use the term slug
    'selected'        => get_query_var( 'breeds' ),
    'class'            => 'breeddropdown',
    'id'              => 'breeds', // Add an ID for JavaScript manipulation if needed
);

    // Get the dropdown HTML
    $select = wp_dropdown_categories( $args );

    // Add onchange attribute to submit form when a breed is selected
    $select = preg_replace( '/<select([^>]*)>/', '<select$1 onchange="this.form.submit()">', $select );

    echo $select;
    ?>

    <noscript><input type="submit" value="View" /></noscript>

</form>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.