1 | <?php |
---|
2 | /** |
---|
3 | * Adds Foo_Widget widget. |
---|
4 | */ |
---|
5 | class Foo_Widget extends WP_Widget { |
---|
6 | |
---|
7 | /** |
---|
8 | * Register widget with WordPress. |
---|
9 | */ |
---|
10 | function __construct() { |
---|
11 | parent::__construct( |
---|
12 | 'foo_widget', // Base ID |
---|
13 | __( 'Widget Title', 'text_domain' ), // Name |
---|
14 | array( 'description' => __( 'A Foo Widget', 'text_domain' ), ) // Args |
---|
15 | ); |
---|
16 | } |
---|
17 | |
---|
18 | /** |
---|
19 | * Front-end display of widget. |
---|
20 | * |
---|
21 | * @see WP_Widget::widget() |
---|
22 | * |
---|
23 | * @param array $args Widget arguments. |
---|
24 | * @param array $instance Saved values from database. |
---|
25 | */ |
---|
26 | public function widget( $args, $instance ) { |
---|
27 | |
---|
28 | echo $args['before_widget']; |
---|
29 | if ( ! empty( $instance['title'] ) ) { |
---|
30 | echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title']; |
---|
31 | } |
---|
32 | echo __( 'Hello, World!', 'text_domain' ); |
---|
33 | echo $args['after_widget']; |
---|
34 | } |
---|
35 | |
---|
36 | /** |
---|
37 | * Back-end widget form. |
---|
38 | * |
---|
39 | * @see WP_Widget::form() |
---|
40 | * |
---|
41 | * @param array $instance Previously saved values from database. |
---|
42 | */ |
---|
43 | public function form( $instance ) { |
---|
44 | $title = ! empty( $instance['title'] ) ? $instance['title'] : 'My Widget'; |
---|
45 | $message = ! empty( $instance['message'] ) ? $instance['message'] : '<i class="icon"></i> This I tag is the source of the problem'; |
---|
46 | ?> |
---|
47 | <p> |
---|
48 | <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> |
---|
49 | <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 ); ?>"> |
---|
50 | </p> |
---|
51 | <p> |
---|
52 | <label for="<?php echo $this->get_field_id( 'message' ); ?>"><?php _e( 'Message:' ); ?></label> |
---|
53 | <input class="widefat" id="<?php echo $this->get_field_id( 'message' ); ?>" name="<?php echo $this->get_field_name( 'message' ); ?>" type="text" value="<?php echo esc_attr( $message ); ?>"> |
---|
54 | </p> |
---|
55 | <?php |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * Sanitize widget form values as they are saved. |
---|
60 | * |
---|
61 | * @see WP_Widget::update() |
---|
62 | * |
---|
63 | * @param array $new_instance Values just sent to be saved. |
---|
64 | * @param array $old_instance Previously saved values from database. |
---|
65 | * |
---|
66 | * @return array Updated safe values to be saved. |
---|
67 | */ |
---|
68 | public function update( $new_instance, $old_instance ) { |
---|
69 | $instance = array(); |
---|
70 | $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; |
---|
71 | $instance['message'] = ( ! empty( $new_instance['message'] ) ) ? wp_kses_post( $new_instance['message'] ) : ''; |
---|
72 | |
---|
73 | return $instance; |
---|
74 | } |
---|
75 | |
---|
76 | } // class Foo_Widget |
---|
77 | |
---|
78 | // register Foo_Widget widget |
---|
79 | function register_foo_widget() { |
---|
80 | register_widget( 'Foo_Widget' ); |
---|
81 | } |
---|
82 | add_action( 'widgets_init', 'register_foo_widget' ); |
---|