Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bappi-d-great/13994cab69065e0220c5 to your computer and use it in GitHub Desktop.
Save bappi-d-great/13994cab69065e0220c5 to your computer and use it in GitHub Desktop.
Search by category add on for WPMU Events+
<?php
/*
Plugin Name: Search by Category
Description: Add a search form where users can search by category
Plugin URI: http://premium.wpmudev.org/project/events-and-booking
Version: 1.0
AddonType: Events
Author: WPMU DEV
*/
class Eab_Events_SearchByCategory {
private function __construct () {
add_shortcode( 'eab_search_by_cat', array( $this, 'eab_search_by_cat_cb' ) );
add_filter( 'the_content', array( $this, 'show_search_content' ) );
}
public static function serve () {
$me = new Eab_Events_SearchByCategory;
}
public function eab_search_by_cat_cb( $atts ) {
$atts = shortcode_atts( array(
'search_term' => '',
'cat_id' => '',
), $atts );
$terms = get_terms( 'eab_events_category' );
$html = '<div class="eab_search_by_cat">';
$html .= '<form action="" method="get">';
$html .= '<div class="eab_main">';
$html .= '<div class="eab_main_col">';
$html .= '<input type="text" name="ev_s" value="' . $atts['search_term'] . '">';
$html .= '</div>';
$html .= '<div class="eab_main_col">';
$html .= '<select name="ev_s_cat">';
$html .= '<option value="">Select a category</option>';
foreach( $terms as $term ){
$html .= '<option ' . ( ( $atts['cat_id'] == $term->term_id ) ? 'selected="selected"' : '' ) . ' value="' . $term->term_id . '">' . $term->name . '</option>';
}
$html .= '</select>';
$html .= '</div>';
$html .= '<div class="eab_main_col">';
$html .= '<input type="submit" name="ev_search_submit">';
$html .= '</div>';
$html .= '</div>';
$html .= '</form>';
$html .= '</div>';
$html .= '<style>';
$html .= '.eab_main_col{width: 33%; float: left; box-sizing: border-box;}';
$html .= '.eab_main{overflow: hidden;}';
$html .= '.eab_search_by_cat{margin-bottom: 20px;}';
$html .= '</style>';
return $html;
}
public function show_search_content( $content ) {
$html = '';
if( isset( $_REQUEST['ev_s'] ) ){
$html .= '<div class="eab_search_wrap">';
$html .= '<div class="search_form">';
$html .= do_shortcode( '[eab_search_by_cat search_term=' . $_REQUEST['ev_s'] . ' cat_id=' . $_REQUEST['ev_s_cat'] . ']' );
$html .= '</div>';
$html .= '<div class="search_result">';
if( ! empty( $_REQUEST['ev_s_cat'] ) ){
$args = array(
'post_type' => 'incsub_event',
's' => $_REQUEST['ev_s'],
'tax_query' => array(
array(
'taxonomy' => 'eab_events_category',
'field' => 'term_id',
'terms' => $_REQUEST['ev_s_cat']
)
)
);
}
else{
$args = array(
'post_type' => 'incsub_event',
's' => $_REQUEST['ev_s']
);
}
$search_query = new WP_Query( $args );
$posts = $search_query->posts;
if( count( $posts ) < 1 ) $html .= '<p>No events found.</p>';
else{
$html .= '<ul>';
foreach( $posts as $post ){
$html .= '<li>';
$html .= '<a href="' . get_permalink( $post->ID ) . '">';
$html .= $post->post_title;
$html .= '</a>';
$html .= '</li>';
}
$html .= '</ul>';
}
$html .= '</div>';
$html .= '</div>';
$html = apply_filters( 'eab_search_events_by_cat_html', $html, $posts );
}else{
$html = $content;
}
return $html;
}
}
Eab_Events_SearchByCategory::serve();
/**
* Adds Event Search by Category widget.
*/
class EAB_search_by_cat_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'EAB_search_by_cat_Widget',
'Search Events by Category',
array( 'description' => 'A widget to search events by category', )
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
}
echo do_shortcode( '[eab_search_by_cat search_term=' . ( isset( $_REQUEST['ev_s'] ) ? $_REQUEST['ev_s'] : '' ) . ' cat_id=' . ( isset( $_REQUEST['ev_s_cat'] ) ? $_REQUEST['ev_s_cat'] : '' ) . ']' );
echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Widget title', 'text_domain' );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
function register_EAB_search_by_cat_Widget_widget() {
register_widget( 'EAB_search_by_cat_Widget' );
}
add_action( 'widgets_init', 'register_EAB_search_by_cat_Widget_widget' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment