Skip to content

Instantly share code, notes, and snippets.

@mattiasghodsian
Last active April 28, 2023 08:36
Show Gist options
  • Select an option

  • Save mattiasghodsian/d911e43eb5d368d892cad711d17a66d1 to your computer and use it in GitHub Desktop.

Select an option

Save mattiasghodsian/d911e43eb5d368d892cad711d17a66d1 to your computer and use it in GitHub Desktop.
Custom Wordpress Dropdown
/**
* Title: Custom Wordpress Dropdown
* Author: Mattias Ghodsian
* Description: Custom wordpress dropdown for taxonomy terms
* Donate a cup of coffee: https://www.buymeacoffee.com/mattiasghodsian
* Donate Eth: 0xBBB96204E45D11C9799c6B12E6eE6F0d4A071Ef5
*
*
* @param array $taxonomy
* @param string $value typ of value to return, strings containts urls works.
* @param string $selected any data to select.
* @param string $name will be applyed to id,name and class
* @param string $show_option_none
*
* @return html
*/
private function custom_wp_dropdown($taxonomy, $value = 'slug', $selected , $name, $show_option_none = '' )
{
$data = get_terms( $taxonomy );
dump($data );
$output = '<select name="'.$name.'" id="'.$name.'" class="'.$name.'" style="width: 100%;">';
if ( !empty($show_option_none) ) {
$output .= '<option value="">'.$show_option_none.'</option>';
}
foreach ($data as $key => $item) {
$output .= '<option value="'.$item->$value.'" '.($selected == $item->$value ? 'selected="selected"' : '').'>'.$item->name.'</option>';
}
$output .= '</select>';
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment