4

I am making a widget and I have used the wp_dropdown_categories() function in that widget. What I can't figure out is how to get the selected value from the dropdown.

What variable is the selected stored in?

$args = array('show_option_none' => 'No Category','hide_empty' => 0);
wp_dropdown_categories( $args );
6
  • What do you want to do with the value? As in HTML the selected value is available as selected within the option
    – Sven
    Commented Dec 21, 2014 at 20:57
  • I want to make a category loop based on the chosen category
    – Jamie
    Commented Dec 21, 2014 at 21:50
  • 1
    Maybe you're better of using get_categories() to loop through an array of categories instead :)
    – Sven
    Commented Dec 21, 2014 at 21:58
  • 2
    Your 100th question :)
    – kaiser
    Commented Dec 21, 2014 at 22:55
  • can you post a code snippet to show what you mean Sven ? I have not been able to get this working
    – Jamie
    Commented Dec 22, 2014 at 2:09

1 Answer 1

9

We can get the categories via get_categories() function (which will get the same categories as wp_dropdown_categories() function), but as array and without the markup.

As the value is returned as array, we can loop through the categories and generate the HTML ourself.

Usually, we would aim for a structure like this:

<select name="categories">
  <option value="1">Category 1</option>
  <option value="2">Category 2</option>
  <option value="3">Category 3</option>
</select>

So we're going to generate the structure from above via PHP by looping through the $categories array:

echo '<select name="categories">';

  // Get categories as array
  $categories = get_categories( $args );
  foreach ( $categories as $category ) :

    echo '<option value="' . $category->term_id . '">' . $category->name . '</option>';

  endforeach;

echo '</select>';

Feel free to modify the function via the $args parameters (see Codex for reference).

Now we'll just need to add the selected attribute within the <option>, so we're going to add this via a short if statement to compare the current $category->term_id and the one saved in the database $stored_category_id:

$stored_category_id = 10; // This is the value from the database
$selected = ( $stored_category_id == $category->term_id  ) ? 'selected' : '';

And that's it! Your complete snippet (maybe with a default option at the beginning) should now look something like this:

echo '<select name="categories">';
  // Add custom option as default
  echo '<option>' . __('No Category', 'text-domain') . '</option>';

  // Get categories as array
  $categories = get_categories( $args );
  foreach ( $categories as $category ) :

    // Check if current term ID is equal to term ID stored in database
    $selected = ( $stored_category_id ==  $category->term_id  ) ? 'selected' : '';

    echo '<option value="' . $category->term_id . '" ' . $selected . '>' . $category->name . '</option>';

  endforeach;

echo '</select>';

Good luck! :)

1
  • 1
    Thanks Sven, My main issue what I wasn't sure how to write up the select in php while getting the values from the database. What really surprises me is the lack of information in the codex.
    – Jamie
    Commented Dec 23, 2014 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.