Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Search Post Type
- Plugin URI: http://www.duncanmorley.co.uk
- Description: A search widget that will only search a selected post type
- Author: Duncan Morley
- Version: 1.0
- Author URI: http://www.duncanmorley.co.uk
- */
- class SearchPostType extends WP_Widget {
- /** constructor */
- function SearchPostType() {
- /* Widget settings. */
- $widget_ops = array(
- 'classname' => 'search-post-type',
- 'description' => 'A search widget that will only search a selected post type'
- );
- /* Widget control settings. */
- $control_ops = array(
- 'width' => 350,
- 'height' => 350,
- 'id_base' => 'search-post-type'
- );
- /* Create the widget. */
- $this->WP_Widget( 'search-post-type', 'Search Post Type', $widget_ops, $control_ops );
- // my code 1
- add_filter('pre_get_posts', array($this,'mySearchFilter'));
- }
- // begin my code 2
- function mySearchFilter($query) {
- $post_type = $_GET['post_type'];
- if (!$post_type) {
- $post_type = 'any';
- }
- if ($query->is_search) {
- $query->set('post_type', $post_type);
- };
- return $query;
- }
- // end mycode 2
- /** @see WP_Widget::widget */
- function widget($args, $instance) {
- extract( $args );
- /* Our variables from the widget settings */
- $title = apply_filters('widget_title', $instance['title'] );
- $text = $instance['text'];
- $button = $instance['button'];
- $type = $instance['type'];
- echo $before_widget;
- if ( $title )
- echo $before_title . $title . $after_title; ?>
- <form method="get" id="searchform" class="clearfix" action="<?php echo esc_url( home_url( '/' ) ); ?>">
- <input type="text" class="field" name="s" id="s" value="<?php if ( $text ) echo $text; ?>" onblur="if (this.value == ''){this.value = '<?php if ( $text ) echo $text; ?>'; }" onfocus="if (this.value == '<?php if ( $text ) echo $text; ?>') {this.value = ''; }" />
- <input type="submit" class="submit" value="<?php if ( $button ) echo $button; ?>" />
- <input type="hidden" name="post_type" value="<?php if ( $type ) echo $type; ?>" />
- </form>
- <?php echo $after_widget;
- }
- /** @see WP_Widget::update */
- function update( $new_instance, $old_instance ) {
- $instance = $old_instance;
- /* Strip tags (if needed) and update the widget settings. */
- $instance['title'] = strip_tags($new_instance['title']);
- $instance['text'] = strip_tags($new_instance['text']);
- $instance['button'] = strip_tags($new_instance['button']);
- $instance['type'] = strip_tags($new_instance['type']);
- return $instance;
- }
- /** @see WP_Widget::form */
- function form($instance) {
- /* Set up some default widget settings. */
- $defaults = array(
- 'title'=> __('', 'search-post-type'),
- 'text' => __('Search', 'search-post-type'),
- 'button' => __('Submit', 'search-post-type'),
- 'type' => __('', 'search-post-type')
- );
- $instance = wp_parse_args( (array) $instance, $defaults ); ?>
- <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 $instance['title']; ?>" />
- </p>
- <p>
- <label for="<?php echo $this->get_field_id('link'); ?>"><?php _e('Search Text:'); ?></label>
- <input class="widefat" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>" type="text" value="<?php echo $instance['text']; ?>" />
- </p>
- <p>
- <label for="<?php echo $this->get_field_id('button'); ?>"><?php _e('Submit Text:'); ?></label>
- <input class="widefat" id="<?php echo $this->get_field_id('button'); ?>" name="<?php echo $this->get_field_name('button'); ?>" type="text" value="<?php echo $instance['button']; ?>" />
- </p>
- <p>
- <label for="<?php echo $this->get_field_id('type'); ?>"><?php _e('Post Type:'); ?></label>
- <select id="<?php echo $this->get_field_id('type'); ?>" name="<?php echo $this->get_field_name('type'); ?>" class="widefat" style="width:100%;">
- <option <?php if ( 'any' == $instance['type'] ) echo 'selected="selected"'; ?> value="any">any</option>
- <?php
- $args=array(
- 'public' => true
- );
- $post_types=get_post_types($args,'names');
- foreach ($post_types as $post_type ) {
- $selected = $post_type == $instance['type'] ? 'selected="selected"' : "";
- echo '<option '.$selected.' value='.$post_type.'>'.$post_type.'</option>';
- } ?>
- </select>
- </p>
- <?php
- }
- } // class SearchPostType
- // mycode 3
- add_action( 'widgets_init', create_function( '', 'register_widget("SearchPostType");' ) );
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement