Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdigital/42ba864f9d11090627fe to your computer and use it in GitHub Desktop.
Save chrisdigital/42ba864f9d11090627fe to your computer and use it in GitHub Desktop.
<?php
//Drop this in your functions.php file in a safe place
//Change all references to "borough" or "Borough" to the taxonomy you need (pay attention to the correct capitalization)
//Then save the edited functions.php
//Find this new filter in: WP Admin > Events > Settings > Filterbar
//Docs for TribeEventsFilter: http://docs.tri.be/Filter-Bar/class-TribeEventsFilter.html
//
//
//This addresses adding/registering the taxonomy to WordPress and the filter functionality to Events Calendar Pro Filter Bar in one shot
//
//
//
// Hook into the init action and call create_borough_taxonomies when it fires
add_action( 'init', 'create_borough_taxonomies', 0 );
// create taxonomy, for the post type "tribe_events"
function create_borough_taxonomies() {
//The Following based on http://codex.wordpress.org/Function_Reference/register_taxonomy
register_taxonomy( 'borough', array( 'tribe_events' ), $args );
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
'name' => _x( 'Boroughs', 'taxonomy general name' ),
'singular_name' => _x( 'Borough', 'taxonomy singular name' ),
'search_items' => __( 'Search Boroughs' ),
'popular_items' => __( 'Popular Boroughs' ),
'all_items' => __( 'All Boroughs' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Borough' ),
'update_item' => __( 'Update Borough' ),
'add_new_item' => __( 'Add New Borough' ),
'new_item_name' => __( 'New Borough Name' ),
'separate_items_with_commas' => __( 'Separate boroughs with commas' ),
'add_or_remove_items' => __( 'Add or remove boroughs' ),
'choose_from_most_used' => __( 'Choose from the most used boroughs' ),
'not_found' => __( 'No boroughs found.' ),
'menu_name' => __( 'Boroughs' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'borough' ),
);
register_taxonomy( 'borough', 'tribe_events', $args );
}
/**
* Adding a custom filter
* A simple copy and paste of the existing Category filter
* Find this new filter in: WP Admin > Events > Settings > Filterbar
* Docs for TribeEventsFilter: http://docs.tri.be/Filter-Bar/class-TribeEventsFilter.html
*/
class TribeEventsFilter_CustomBoroughs extends TribeEventsFilter {
public $type = 'select';
public function get_admin_form() {
$title = $this->get_title_field();
$type = $this->get_type_field();
return $title.$type;
}
protected function get_type_field() {
$name = $this->get_admin_field_name('type');
$field = sprintf( __( 'Type: %s %s', 'tribe-events-filter-view' ),
sprintf( '<label><input type="radio" name="%s" value="select" %s /> %s</label>',
$name,
checked( $this->type, 'select', false ),
__( 'Dropdown', 'tribe-events-filter-view' )
),
sprintf( '<label><input type="radio" name="%s" value="checkbox" %s /> %s</label>',
$name,
checked( $this->type, 'checkbox', false ),
__( 'Checkboxes', 'tribe-events-filter-view' )
)
);
return '<div class="tribe_events_active_filter_type_options">'.$field.'</div>';
}
protected function get_values() {
$event_boroughs = array();
$event_boroughs_term = get_terms( 'borough', array( 'orderby' => 'name', 'order' => 'DESC' ) );
$event_boroughs_by_id = array();
foreach( $event_boroughs_term as $term ) {
$event_boroughs_by_id[$term->term_id] = $term;
}
$event_boroughs_by_id_reverse = array_reverse( $event_boroughs_by_id );
$parents = array( '0' );
while ( !empty( $parents ) ) {
$parents_copy = $parents;
foreach ( $event_boroughs_by_id_reverse as $term ) {
if ( in_array( $term->parent, $parents_copy ) ) {
$parents[] = $term->term_id;
unset( $event_boroughs_by_id[$term->term_id] );
$event_boroughs_by_id = TribeEvents::array_insert_after_key( $term->parent, $event_boroughs_by_id, array( $term->term_id => $term ) );
}
}
$parents = array_diff( $parents, $parents_copy );
}
$child_spacer = '&nbsp;&nbsp;';
foreach( $event_boroughs_by_id as $cat ) {
$child_depth = 0;
$parent_id = $cat->parent;
while ( $parent_id != 0 ) {
$child_depth++;
$parent_id = $event_boroughs_by_id[$parent_id]->parent;
}
$child_indent = str_repeat($child_spacer, $child_depth);
$event_boroughs[] = array(
'name' => $child_indent . $cat->name,
'value' => $cat->term_id,
'data' => array(
'slug' => $cat->slug,
),
);
}
return $event_boroughs;
}
protected function setup_query_args() {
$this->queryArgs = array( 'tax_query' => array( array(
'taxonomy' => 'borough',
'field' => 'id',
'terms' => $this->currentValue,
'include_children' => false,
) ) );
}
}
// This adds our new filter to the Filterbar options
// Invokes TribeEventsFilter::__construct($name, $slug);
function CustomBoroughs_taxfilter(){
new TribeEventsFilter_CustomBoroughs('Borough', 'borough');
}
add_action('tribe_events_filters_create_filters','CustomBoroughs_taxfilter');
@nicktea
Copy link

nicktea commented Sep 17, 2015

Hi,
Thanks great mod for the calendar but I am getting a few deprecated errors on the backend and front end

Backend and Frontend
Notice: /home/kilve/public_html/beta/wp-content/plugins/the-events-calendar-filterbar/src/deprecated/TribeEventsFilter.php is deprecated since version 3.10! Use Tribe__Events__Filterbar__Filter instead. in /home/kilve/public_html/beta/wp-includes/functions.php on line 3510

Notice: Undefined variable: args in /home/kilve/public_html/beta/wp-content/themes/KilveCourt/functions.php on line 584

Frontend
Notice: /home/kilve/public_html/beta/wp-content/plugins/the-events-calendar/src/deprecated/TribeEvents.php is deprecated since version 3.10! Use Tribe__Events__Main instead. in /home/kilve/public_html/beta/wp-includes/functions.php on line 3510

Any idea how to fix this?

I am using the following versions:
The Events Calendar = 3.12.1
The Events Calendar PRO = 3.12
The Events Calendar: Filter Bar = 3.12

Thanks
Nick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment