I have a PHP function which returns the values in a PHP list() format. This function returns a list of WordPress taxonomies with a couple of custom entries added manually.
/**
* Returns array with taxonomies
*/
public function get_taxonomies(){
$args = array(
'public' => true,
'_builtin' => false
);
$taxonomies = get_taxonomies($args, 'objects');
$list = array();
$list[__('Category', $this->prefix)] = 'category';
$list[__('Tag', $this->prefix)] = 'post_tag';
foreach($taxonomies as $tax){
$list[$tax->labels->name] = $tax->name;
}
return $list;
}
This function returns the following array values:
Array
(
[Category] => category
[Tag] => post_tag
[Product Categories] => product_cat
[Product Tags] => product_tag
[Shipping Classes] => product_shipping_class
)
The first part of the array is the taxonomy name and the second the taxonomy value.
I display these values as a checkbox list and save them in my database as an array.
This is the data that I save in the database for the list of checkboxes. This is what is returned if I print_r() the save data for the list:
Array
(
[category] => category
[post_tag] => post_tag
[product_cat] => product_cat
)
Using the WordPress checked() function I am trying to return the initial list of taxonomies with the values saved in the database marked as HTML "checked".
Here is what I have tried:
// This is where I am trying to get at the saved values
$multi_stored = $options[$id];
$taxonomies = $this->get_taxonomies();
foreach($taxonomies as $name => $label){
$check_key = $id . '_' . $option;
$output .= '<input class="checkbox'.$field_class.'" type="checkbox" name="'.$this->prefix.'_options['.$id.']['.$label.']" id="'.$check_key.'" value="'.$label.'" '.checked($multi_stored[$label], $label, false ).' /><label for="'.esc_attr($name).'">'.esc_html($name).'</label><br />';
}
When I run this code I received the following PHP warning.
Notice: Undefined index: product_tag in C:\xampp\htdocs\wpbp-admin.php on line ...
It is giving me this warning for every item in the checkbox list that is NOT checked.
I would like to have the code not return these warnings. I understand it is a matter of using isset but I am not sure where to place it.
Any help is greatly appreciated, thanks.