1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | Plugin Name: JB Widget Problems Demo |
---|
5 | Plugin URI: http://johnbeales.com |
---|
6 | Description: Shows a problem where Widgets are not displayed if they don't have any $instance data set. |
---|
7 | Version: 1.0 |
---|
8 | Author: John Beales |
---|
9 | Author URI: http://johnbeales.com |
---|
10 | License: GPL |
---|
11 | */ |
---|
12 | |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | |
---|
17 | /** |
---|
18 | * Adds JB_Good_Widget, a widget that displays as expected. |
---|
19 | */ |
---|
20 | class JB_Good_Widget extends WP_Widget { |
---|
21 | |
---|
22 | /** |
---|
23 | * Register widget with WordPress. |
---|
24 | */ |
---|
25 | function __construct() { |
---|
26 | parent::__construct( |
---|
27 | 'jb_good_widget', // Base ID |
---|
28 | __( 'Good Widget', 'jb_widgets' ), // Name |
---|
29 | array( 'description' => __( 'This widget will appear as expected, because we save a title for it.', 'jb_widgets' ), ) // Args |
---|
30 | ); |
---|
31 | } |
---|
32 | |
---|
33 | /** |
---|
34 | * Front-end display of widget. |
---|
35 | * |
---|
36 | * @see WP_Widget::widget() |
---|
37 | * |
---|
38 | * @param array $args Widget arguments. |
---|
39 | * @param array $instance Saved values from database. |
---|
40 | */ |
---|
41 | public function widget( $args, $instance ) { |
---|
42 | echo $args['before_widget']; |
---|
43 | if ( ! empty( $instance['title'] ) ) { |
---|
44 | echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title']; |
---|
45 | } |
---|
46 | echo __( 'I am a slightly-configurable widget, because you can set my title. Setting my title makes me visible.', 'jb_widgets' ); |
---|
47 | echo $args['after_widget']; |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * Back-end widget form. |
---|
52 | * |
---|
53 | * @see WP_Widget::form() |
---|
54 | * |
---|
55 | * @param array $instance Previously saved values from database. |
---|
56 | */ |
---|
57 | public function form( $instance ) { |
---|
58 | $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'New title', 'text_domain' ); |
---|
59 | ?> |
---|
60 | <p> |
---|
61 | <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> |
---|
62 | <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 ); ?>"> |
---|
63 | </p> |
---|
64 | <?php |
---|
65 | var_dump( $instance ); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Sanitize widget form values as they are saved. |
---|
70 | * |
---|
71 | * @see WP_Widget::update() |
---|
72 | * |
---|
73 | * @param array $new_instance Values just sent to be saved. |
---|
74 | * @param array $old_instance Previously saved values from database. |
---|
75 | * |
---|
76 | * @return array Updated safe values to be saved. |
---|
77 | */ |
---|
78 | public function update( $new_instance, $old_instance ) { |
---|
79 | $instance = array(); |
---|
80 | $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; |
---|
81 | |
---|
82 | return $instance; |
---|
83 | } |
---|
84 | |
---|
85 | } // class Good Widget |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | |
---|
90 | /** |
---|
91 | * Adds JB_Bad_Widget, a widget that DOES NOT display as expected |
---|
92 | */ |
---|
93 | class JB_Bad_Widget extends WP_Widget { |
---|
94 | |
---|
95 | /** |
---|
96 | * Register widget with WordPress. |
---|
97 | */ |
---|
98 | function __construct() { |
---|
99 | parent::__construct( |
---|
100 | 'jb_bad_widget', // Base ID |
---|
101 | __( 'Bad Widget', 'text_domain' ), // Name |
---|
102 | array( 'description' => __( 'A widget that should appear, but doesn\'t', 'jb_widgets' ), ) // Args |
---|
103 | ); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * Front-end display of widget. |
---|
108 | * |
---|
109 | * @see WP_Widget::widget() |
---|
110 | * |
---|
111 | * @param array $args Widget arguments. |
---|
112 | * @param array $instance Saved values from database. |
---|
113 | */ |
---|
114 | public function widget( $args, $instance ) { |
---|
115 | echo $args['before_widget']; |
---|
116 | |
---|
117 | echo $args['before_title'] . apply_filters( 'widget_title', 'Hi there,' ). $args['after_title']; |
---|
118 | |
---|
119 | echo __( 'I am a static widget with no configurable options.', 'jb_widgets' ); |
---|
120 | echo $args['after_widget']; |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * Back-end widget form. |
---|
125 | * |
---|
126 | * @see WP_Widget::form() |
---|
127 | * |
---|
128 | * @param array $instance Previously saved values from database. |
---|
129 | */ |
---|
130 | public function form( $instance ) { |
---|
131 | // Nothing, because there are no user-configurable options for this wedget. |
---|
132 | var_dump( $instance ); |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Sanitize widget form values as they are saved. |
---|
137 | * |
---|
138 | * @see WP_Widget::update() |
---|
139 | * |
---|
140 | * @param array $new_instance Values just sent to be saved. |
---|
141 | * @param array $old_instance Previously saved values from database. |
---|
142 | * |
---|
143 | * @return array Updated safe values to be saved. |
---|
144 | */ |
---|
145 | public function update( $new_instance, $old_instance ) { |
---|
146 | // Nothing, because there are no user-configurable options for this widget. |
---|
147 | } |
---|
148 | |
---|
149 | } // class Good Widget |
---|
150 | |
---|
151 | |
---|
152 | add_action( 'widgets_init', function() { |
---|
153 | register_widget( 'JB_Good_Widget' ); |
---|
154 | register_widget( 'JB_Bad_Widget' ); |
---|
155 | }); |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | |
---|