Advertisement
Guest User

Untitled

a guest
Oct 26th, 2011
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Search Post Type
  4. Plugin URI: http://www.duncanmorley.co.uk
  5. Description: A search widget that will only search a selected post type
  6. Author: Duncan Morley
  7. Version: 1.0
  8. Author URI: http://www.duncanmorley.co.uk
  9. */
  10.  
  11. class SearchPostType extends WP_Widget {
  12.  
  13. /** constructor */
  14. function SearchPostType() {
  15. /* Widget settings. */
  16. $widget_ops = array(
  17. 'classname' => 'search-post-type',
  18. 'description' => 'A search widget that will only search a selected post type'
  19. );
  20.  
  21. /* Widget control settings. */
  22. $control_ops = array(
  23. 'width' => 350,
  24. 'height' => 350,
  25. 'id_base' => 'search-post-type'
  26. );
  27.  
  28. /* Create the widget. */
  29. $this->WP_Widget( 'search-post-type', 'Search Post Type', $widget_ops, $control_ops );
  30.  
  31. // my code 1
  32. add_filter('pre_get_posts', array($this,'mySearchFilter'));
  33.  
  34. }
  35.  
  36. // begin my code 2
  37. function mySearchFilter($query) {
  38.  
  39. $post_type = $_GET['post_type'];
  40. if (!$post_type) {
  41. $post_type = 'any';
  42. }
  43. if ($query->is_search) {
  44. $query->set('post_type', $post_type);
  45. };
  46. return $query;
  47. }
  48. // end mycode 2
  49.  
  50.  
  51. /** @see WP_Widget::widget */
  52. function widget($args, $instance) {
  53. extract( $args );
  54.  
  55. /* Our variables from the widget settings */
  56. $title = apply_filters('widget_title', $instance['title'] );
  57. $text = $instance['text'];
  58. $button = $instance['button'];
  59. $type = $instance['type'];
  60.  
  61. echo $before_widget;
  62.  
  63. if ( $title )
  64. echo $before_title . $title . $after_title; ?>
  65.  
  66. <form method="get" id="searchform" class="clearfix" action="<?php echo esc_url( home_url( '/' ) ); ?>">
  67. <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 = ''; }" />
  68. <input type="submit" class="submit" value="<?php if ( $button ) echo $button; ?>" />
  69. <input type="hidden" name="post_type" value="<?php if ( $type ) echo $type; ?>" />
  70. </form>
  71.  
  72. <?php echo $after_widget;
  73.  
  74. }
  75.  
  76. /** @see WP_Widget::update */
  77. function update( $new_instance, $old_instance ) {
  78. $instance = $old_instance;
  79.  
  80. /* Strip tags (if needed) and update the widget settings. */
  81. $instance['title'] = strip_tags($new_instance['title']);
  82. $instance['text'] = strip_tags($new_instance['text']);
  83. $instance['button'] = strip_tags($new_instance['button']);
  84. $instance['type'] = strip_tags($new_instance['type']);
  85.  
  86. return $instance;
  87. }
  88.  
  89. /** @see WP_Widget::form */
  90. function form($instance) {
  91. /* Set up some default widget settings. */
  92. $defaults = array(
  93. 'title'=> __('', 'search-post-type'),
  94. 'text' => __('Search', 'search-post-type'),
  95. 'button' => __('Submit', 'search-post-type'),
  96. 'type' => __('', 'search-post-type')
  97. );
  98. $instance = wp_parse_args( (array) $instance, $defaults ); ?>
  99.  
  100. <p>
  101. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  102. <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']; ?>" />
  103. </p>
  104. <p>
  105. <label for="<?php echo $this->get_field_id('link'); ?>"><?php _e('Search Text:'); ?></label>
  106. <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']; ?>" />
  107. </p>
  108. <p>
  109. <label for="<?php echo $this->get_field_id('button'); ?>"><?php _e('Submit Text:'); ?></label>
  110. <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']; ?>" />
  111. </p>
  112. <p>
  113. <label for="<?php echo $this->get_field_id('type'); ?>"><?php _e('Post Type:'); ?></label>
  114. <select id="<?php echo $this->get_field_id('type'); ?>" name="<?php echo $this->get_field_name('type'); ?>" class="widefat" style="width:100%;">
  115. <option <?php if ( 'any' == $instance['type'] ) echo 'selected="selected"'; ?> value="any">any</option>
  116. <?php
  117. $args=array(
  118. 'public' => true
  119. );
  120. $post_types=get_post_types($args,'names');
  121. foreach ($post_types as $post_type ) {
  122. $selected = $post_type == $instance['type'] ? 'selected="selected"' : "";
  123. echo '<option '.$selected.' value='.$post_type.'>'.$post_type.'</option>';
  124. } ?>
  125. </select>
  126. </p>
  127.  
  128. <?php
  129.  
  130. }
  131.  
  132. } // class SearchPostType
  133.  
  134. // mycode 3
  135. add_action( 'widgets_init', create_function( '', 'register_widget("SearchPostType");' ) );
  136.  
  137.  
  138. ?>
  139.  
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement