Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?php
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
/** Walker_Category_Checklist class */
require_once( ABSPATH . 'wp-admin/includes/class-walker-category-checklist.php' );
/**
* Extends Walker_Category_Checklist and uses radio input instead of checklist
*
* @since 0.4.1
*/
class WP_Event_Calendar_Walker_Category_Radio extends Walker_Category_Checklist {
/**
* Start the element output.
*
* @see Walker::start_el()
*
* @since 0.4.1
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $category The current term object.
* @param int $depth Depth of the term in reference to parents. Default 0.
* @param array $args An array of arguments. @see wp_terms_checklist()
* @param int $id ID of the current term.
*/
public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
// Note that Walker classes are trusting with their previously
// validated object properties.
$taxonomy = $args['taxonomy'];
$name = 'tax_input[' . $taxonomy . ']';
// Maybe show popular categories tab
$args['popular_cats'] = empty( $args['popular_cats'] )
? array()
: $args['popular_cats'];
// Maybe add popular category class
$class = in_array( $category->term_id, $args['popular_cats'] )
? ' class="popular-category"'
: '';
// Maybe use already selected categories
$args['selected_cats'] = empty( $args['selected_cats'] )
? array()
: $args['selected_cats'];
/** This filter is documented in wp-includes/category-template.php */
$output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" .
'<label class="selectit"><input value="' . $category->name . '" type="radio" name="' . $name . '[]" id="in-' . $taxonomy . '-' . $category->term_id . '"' .
checked( in_array( $category->term_id, $args['selected_cats'] ), true, false ) .
disabled( empty( $args['disabled'] ), false, false ) . ' /> ' .
esc_html( apply_filters( 'the_category', $category->name ) ) . '</label>';
}
}
/**
* Event Types Metabox
* Output radio buttons instead of the default WordPress mechanism
*
* @since 0.4.1
*
* @param array $args
* @param string $taxonomy
*
* @return array
*/
function wp_event_calendar_taxonomy_args( $args = array(), $taxonomy = '' ) {
if ( 'event-type' === $taxonomy ) {
$r = apply_filters( 'wp_event_calendar_taxonomy_args', array(
'meta_box_cb' => 'post_categories_meta_box'
), $args );
$args = wp_parse_args( $args, $r );
}
return $args;
}
/**
* Use the custom walker for radio buttons
*
* @since 0.4.1
*
* @param array $args
*
* @return array
*/
function wp_event_calendar_checklist_args( $args = array() ) {
if ( 'event-type' === $args['taxonomy'] ) {
$r = apply_filters( 'wp_event_calendar_checklist_args', array(
'walker' => new WP_Event_Calendar_Walker_Category_Radio(),
), $args );
$args = wp_parse_args( $args, $r );
}
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment