Skip to content

Instantly share code, notes, and snippets.

@barryhughes
Created July 25, 2017 19:13
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 barryhughes/fa8c1c2bed7a7d4ac340e06ff40c3e78 to your computer and use it in GitHub Desktop.
Save barryhughes/fa8c1c2bed7a7d4ac340e06ff40c3e78 to your computer and use it in GitHub Desktop.
Replacement with categories/tags as checkboxes and hierarchical indents
<?php
// Don't load directly
defined( 'WPINC' ) or die;
$selected_terms = array();
$taxonomy_obj = get_taxonomy( $taxonomy );
$ajax_args = array(
'taxonomy' => $taxonomy,
);
$taxonomy_label = $taxonomy_obj->label;
// If we already have Event on the actual Taxonomy Label
if ( false === strpos( $taxonomy_obj->label, tribe_get_event_label_singular() ) ) {
$taxonomy_label = sprintf( '%s %s', tribe_get_event_label_singular(), $taxonomy_obj->label );
}
// Check we have terms
$has_terms = count( get_terms( $taxonomy, array( 'hide_empty' => false, 'number' => 1, 'fields' => 'ids' ) ) ) < 1;
// Setup selected tags
$value = ! empty( $_POST['tax_input'][ $taxonomy ] ) ? explode( ',', esc_attr( trim( $_POST['tax_input'][ $taxonomy ] ) ) ) : array();
// if no tags from $_POST then look for saved tags
if ( empty( $value ) ) {
$terms = wp_get_post_terms( get_the_ID(), $taxonomy );
$value = wp_list_pluck( $terms, 'term_id' );
}
foreach ( $value as $term_id ) {
$term = get_term( $term_id, $taxonomy );
$selected_terms[ $term_id ] = array(
'id' => $term->term_id,
'text' => $term->name,
);
}
if ( is_array( $value ) ) {
$value = implode( ',', $value );
}
if ( $has_terms ) {
return;
}
$hierarchical_terms_list = function( $taxonomy, $term_parent = 0, $depth = 0 ) use ( &$hierarchical_terms_list ) {
$terms = get_terms( array(
'taxonomy' => $taxonomy,
'get' => 'all',
'parent' => $term_parent
) );
if ( empty( $terms ) ) {
return array();
}
foreach ( $terms as $single_term ) {
$single_term->depth = $depth;
$term_list[] = $single_term;
$children = $hierarchical_terms_list( $taxonomy, $single_term->term_id, $depth + 1 );
if ( is_array( $children ) ) {
$term_list = array_merge( $term_list, $children );
}
}
return $term_list;
};
?>
<div class="tribe-section tribe-section-taxonomy">
<div class="tribe-section-header">
<h3><?php echo esc_html( $taxonomy_label ); ?></h3>
</div>
<?php
/**
* Allow developers to hook and add content to the beginning of this section
* @param string $taxonomy_slug
*/
do_action( 'tribe_events_community_section_before_taxonomy', $taxonomy );
?>
<?php foreach ( $hierarchical_terms_list( $taxonomy ) as $term ): ?>
<label>
<input
type="checkbox"
name="tax_input[<?php echo esc_attr( $taxonomy ); ?>][]"
value="<?php echo esc_attr( $term->term_id ); ?>"
<?php echo isset( $selected_terms[ $term->term_id ] ) ? 'checked="checked"' : ''; ?>
style="margin-left: <?php echo esc_attr( $term->depth * 1.75 ); ?>rem"
/>
<?php echo esc_html( $term->name ); ?>
</label> <br/>
<?php endforeach; ?>
<?php
/**
* Allow developers to hook and add content to the end of this section
* @param string $taxonomy_slug
*/
do_action( 'tribe_events_community_section_after_taxonomy', $taxonomy );
?>
</div>
@barryhughes
Copy link
Author

Hacky! Moving the logic used to get the term depth out from the template and something cleaner than setting the left margin via the style attribute would be nicer.

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