forked from tinysou/tinysou-wordpress
/
class-tinysou-widget.php
72 lines (64 loc) · 2.37 KB
/
class-tinysou-widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
*
* @author Tairy <tairyguo@gmail.com>
*
* @since 1.0
*
*/
class Tinysou_Search_Widget extends WP_Widget {
function __construct() {
$widget_ops = array( 'classname' => 'tinysou_serarch_widget', 'description' => __( 'Search content in a specific category' ) );
parent::__construct( 'tinysou_search_widget', __( 'Category Filtered Search' ), $widget_ops );
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
$category = $instance['category'];
echo $before_widget;
if( !empty( $title ) ) {
echo $before_title . $title . $after_title;
}
$form = '<form role="search" method="get" id="searchform" action="' . esc_url( home_url( '/' ) ) . '" >
<div><label class="screen-reader-text" for="s">' . __( 'Search for:' ) . '</label>
<input type="text" value="' . get_search_query() . '" name="s" id="s" />
<input type="hidden" value="' . $category . '" name="st-cat" id="st-cat" />
<input type="submit" id="searchsubmit" value="'. esc_attr__( 'Search' ) .'" />
</div>
</form>';
echo $form;
echo $after_widget;
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'category' => 0 ) );
$title = $instance['title'];
$category = $instance['category'];
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?>
<input class="regular-text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Category ID:' ); ?>
<?php
$settings = array(
'name' => $this->get_field_name( 'category' ),
'id' => $this->get_field_id( 'category' ),
'selected' => $category
);
wp_dropdown_categories( $settings );
?>
</label>
</p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '', 'category' => 0 ) );
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['category'] = intval( $new_instance['category'] );
return $instance;
}
}
add_action( 'widgets_init', create_function( '', 'register_widget( "tinysou_search_widget" );' ) );