Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Category Drop Down in Plugin Options WordPress

  • SOLVED

I would like to build a category drop down into my plugin options. I can't seem to find the right code to do this. I think I'm almost there, but it not quite.



<select name='radar_options[blog_category]'>
<?php
$categories=get_categories();
foreach($categories as $category) {

$selected = '';
$selected = 'selected="selected"';

echo '<option value="' . $radar_options['blog_category'] . '" ' . $selected . '>' . $category->name . '</option>';


}
?>
</select>




Any help would be appreciated!

Mike

Answers (2)

2012-06-29

Luis Abarca answers:


wp_dropdown_categories


[[LINK href="http://codex.wordpress.org/Function_Reference/wp_dropdown_categories"]]http://codex.wordpress.org/Function_Reference/wp_dropdown_categories[[/LINK]]


Mike McAlister comments:

No, not that easy. This is for plugin settings, so you can't just paste wp_dropdown_categories in there.

Mike


Luis Abarca comments:

Just get the category from your plugin options



$radar_options = get_option('radar_options');

$category_selected = $radar_options['blog_category'];

wp_dropdown_categories(array('name' => "radar_options[blog_category]", 'selected' => $category_selected) );


Mike McAlister comments:

This does bring in the categories as it should, but it doesn't seem to be saving. I've tried to echo out the blog_category setting and it returns blank.


Mike McAlister comments:

Apologies, it DOES work. I had another option resetting it by accident. Good stuff, Luis.

2012-06-29

Kailey Lampert answers:

Luis' answer looks good to me. Where's it falling short for your needs?


Mike McAlister comments:

This does bring in the categories as it should, but it doesn't seem to be saving. I've tried to echo out the blog_category setting and it returns blank.


Mike McAlister comments:

He was right, I had a stupid piece of duplicate code screwing things up. D'oh!