eventsmanager

Custom Taxonomy Example

Jul 22nd, 2015
479
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3.  This snippet will create a custom taxonomy, register it to the event CPT, and add a search field to your events search form.
  4.  By Events Manager 5.5.8 the last two functions aren't needed, because EM will automatically register these new taxonomies as searchable within itself.
  5.  
  6.  For help with adding this for your site, see here - http://wp-events-plugin.com/tutorials/how-to-safely-add-php-code-to-wordpress/
  7.  */
  8.  
  9. //Create a custom taxonomy.
  10. function my_em_create_custom_taxonomy() {
  11.     $labels = array(
  12.         'name' => 'Grades',
  13.         'singular_name' => 'Grade',
  14.         'search_items' => 'Search Grades',
  15.         'all_items' => 'All Grades',
  16.         'parent_item' => 'Parent Grade',
  17.         'parent_item_colon' => 'Parent Grade:',
  18.         'edit_item' => 'Edit Grade',
  19.         'update_item' => 'Update Grade',
  20.         'add_new_item' => 'Add New Grade',
  21.         'new_item_name' => 'New Grade Name',
  22.         'menu_name' => 'Grade Categories',
  23.     );
  24.     register_taxonomy('grades',array(EM_POST_TYPE_EVENT), array(
  25.         'hierarchical' => true,
  26.         'labels' => $labels,
  27.         'query_var' => true
  28.     ));
  29. }
  30. add_action( 'init', 'my_em_create_custom_taxonomy', 0 );
  31.  
  32. //Hook into the search form and add the Custom Taxonomy search field.
  33. function my_em_custom_taxonomy_search_form(){
  34.     ?>
  35.     <div class="em-search-category em-search-field">
  36.     <label>Grades</label>
  37.     <?php
  38.     $selected = !empty($_REQUEST['grades']) ? $_REQUEST['grades']:false;
  39.     wp_dropdown_categories( array(
  40.         'hide_empty' => 0,
  41.         'orderby' =>'name',
  42.         'name' => 'grades',
  43.         'hierarchical' => true,
  44.         'taxonomy' => 'grades',
  45.         'selected' => $selected,
  46.         'show_option_none' => 'Choose a grade',
  47.         'option_none_value'=>0,
  48.         'class'=>'em-events-search-grade',
  49.     ));
  50.     ?>
  51.     </div>
  52.     <?php
  53. }
  54. add_action('em_template_events_search_form_footer', 'my_em_custom_taxonomy_search_form');
  55.  
  56. //Delete everthing below in Events Manager 5.5.8 or later, although this has no negative impact if left here by mistake
  57.  
  58. function my_em_custom_taxonomy_accepted_search($searches){
  59.     $searches[] = 'grades';
  60.     return $searches;
  61. }
  62. add_filter('em_accepted_searches','my_em_custom_taxonomy_accepted_search',1,1);
  63.  
  64. function my_em_custom_taxonomy_default_search( $args ){
  65.     if( empty($args['grades']) ) $args['grades'] = false;
  66.     return $args;
  67. }
  68. add_filter('em_events_get_default_search','my_em_custom_taxonomy_default_search');
RAW Paste Data