26
public $id_base; // Root id for all widgets of this type.
27
public $name; // Name for this widget type.
28
public $widget_options; // Option array passed to wp_register_sidebar_widget()
29
public $control_options; // Option array passed to wp_register_widget_control()
31
public $number = false; // Unique ID number of the current instance.
32
public $id = false; // Unique ID string of the current instance (id_base-number)
33
public $updated = false; // Set true when we update the data after a POST submit - makes sure we don't do it twice.
27
* Root ID for all widgets of this type.
36
* Name for this widget type.
45
* Option array passed to {@see wp_register_sidebar_widget()}.
51
public $widget_options;
54
* Option array passed to {@see wp_register_widget_control()}.
60
public $control_options;
63
* Unique ID number of the current instance.
69
public $number = false;
72
* Unique ID string of the current instance (id_base-number).
81
* Whether the widget data has been updated.
83
* Set to true when the data is updated after a POST submit - ensures it does
90
public $updated = false;
35
92
// Member functions that you must over-ride.
37
/** Echo the widget content.
95
* Echo the widget content.
39
97
* Subclasses should over-ride this function to generate their widget code.
41
* @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
42
* @param array $instance The settings for the particular instance of the widget
102
* @param array $args Display arguments including before_title, after_title,
103
* before_widget, and after_widget.
104
* @param array $instance The settings for the particular instance of the widget.
44
public function widget($args, $instance) {
106
public function widget( $args, $instance ) {
45
107
die('function WP_Widget::widget() must be over-ridden in a sub-class.');
48
/** Update a particular instance.
50
* This function should check that $new_instance is set correctly.
51
* The newly calculated value of $instance should be returned.
52
* If "false" is returned, the instance won't be saved/updated.
54
* @param array $new_instance New settings for this instance as input by the user via form()
55
* @param array $old_instance Old settings for this instance
56
* @return array Settings to save or bool false to cancel saving
111
* Update a particular instance.
113
* This function should check that $new_instance is set correctly. The newly-calculated
114
* value of `$instance` should be returned. If false is returned, the instance won't be
120
* @param array $new_instance New settings for this instance as input by the user via
121
* {@see WP_Widget::form()}.
122
* @param array $old_instance Old settings for this instance.
123
* @return array Settings to save or bool false to cancel saving.
58
public function update($new_instance, $old_instance) {
125
public function update( $new_instance, $old_instance ) {
59
126
return $new_instance;
62
/** Echo the settings update form
64
* @param array $instance Current settings
130
* Output the settings update form.
135
* @param array $instance Current settings.
136
* @return string Default return is 'noform'.
66
138
public function form($instance) {
67
139
echo '<p class="no-options-widget">' . __('There are no options for this widget.') . '</p>';
71
143
// Functions you'll need to call.
76
* @param string $id_base Optional Base ID for the widget, lower case,
77
* if left empty a portion of the widget's class name will be used. Has to be unique.
78
* @param string $name Name for the widget displayed on the configuration page.
79
* @param array $widget_options Optional Passed to wp_register_sidebar_widget()
80
* - description: shown on the configuration page
82
* @param array $control_options Optional Passed to wp_register_widget_control()
83
* - width: required if more than 250px
84
* - height: currently not used but may be needed in the future
151
* @param string $id_base Optional Base ID for the widget, lowercase and unique. If left empty,
152
* a portion of the widget's class name will be used Has to be unique.
153
* @param string $name Name for the widget displayed on the configuration page.
154
* @param array $widget_options Optional. Widget options. See {@see wp_register_sidebar_widget()} for
155
* information on accepted arguments. Default empty array.
156
* @param array $control_options Optional. Widget control options. See {@see wp_register_widget_control()}
157
* for information on accepted arguments. Default empty array.
86
159
public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
87
160
$this->id_base = empty($id_base) ? preg_replace( '/(wp_)?widget_/', '', strtolower(get_class($this)) ) : strtolower($id_base);
114
* Constructs id attributes for use in form() fields
116
* This function should be used in form() methods to create id attributes for fields to be saved by update()
118
* @param string $field_name Field name
119
* @return string ID attribute for $field_name
192
* Constructs id attributes for use in {@see WP_Widget::form()} fields.
194
* This function should be used in form() methods to create id attributes
195
* for fields to be saved by {@see WP_Widget::update()}.
200
* @param string $field_name Field name.
201
* @return string ID attribute for `$field_name`.
121
public function get_field_id($field_name) {
203
public function get_field_id( $field_name ) {
122
204
return 'widget-' . $this->id_base . '-' . $this->number . '-' . $field_name;
125
// Private Functions. Don't worry about these.
208
* Register all widget instances of this widget class.
127
213
public function _register() {
128
214
$settings = $this->get_settings();
167
* Determine if we're in the Customizer; if true, then the object cache gets
168
* suspended and widgets should check this to decide whether they should
169
* store anything persistently to the object cache, to transients, or
262
* Determine whether the current request is inside the Customizer preview.
264
* If true -- the current request is inside the Customizer preview, then
265
* the object cache gets suspended and widgets should check this to decide
266
* whether they should store anything persistently to the object cache,
267
* to transients, or anywhere else.
174
* @return bool True if Customizer is on, false if not.
272
* @return bool True if within the Customizer preview, false if not.
176
274
public function is_preview() {
177
275
global $wp_customize;
178
276
return ( isset( $wp_customize ) && $wp_customize->is_preview() ) ;
181
/** Generate the actual widget content.
182
* Just finds the instance and calls widget().
183
* Do NOT over-ride this function. */
280
* Generate the actual widget content (Do NOT override).
282
* Finds the instance and calls {@see WP_Widget::widget()}.
287
* @param array $args Display arguments. See {@see WP_Widget::widget()} for information
288
* on accepted arguments.
289
* @param int|array $widget_args {
290
* Optional. Internal order number of the widget instance, or array of multi-widget arguments.
293
* @type int $number Number increment used for multiples of the same widget.
184
296
public function display_callback( $args, $widget_args = 1 ) {
185
297
if ( is_numeric($widget_args) )
186
298
$widget_args = array( 'number' => $widget_args );
365
/** Helper function: Registers a single instance. */
366
public function _register_one($number = -1) {
482
* Register an instance of the widget class.
487
* @param integer $number Optional. The unique order number of this widget instance
488
* compared to other instances of the same class. Default -1.
490
public function _register_one( $number = -1 ) {
367
491
wp_register_sidebar_widget( $this->id, $this->name, $this->_get_display_callback(), $this->widget_options, array( 'number' => $number ) );
368
492
_register_widget_update_callback( $this->id_base, $this->_get_update_callback(), $this->control_options, array( 'number' => -1 ) );
369
493
_register_widget_form_callback( $this->id, $this->name, $this->_get_form_callback(), $this->control_options, array( 'number' => $number ) );
372
public function save_settings($settings) {
497
* Save the settings for all instances of the widget class.
502
* @param array $settings Multi-dimensional array of widget instance settings.
504
public function save_settings( $settings ) {
373
505
$settings['_multiwidget'] = 1;
374
506
update_option( $this->option_name, $settings );
510
* Get the settings for all instances of the widget class.
515
* @return array Multi-dimensional array of widget instance settings.
377
517
public function get_settings() {
378
519
$settings = get_option($this->option_name);
380
521
if ( false === $settings && isset($this->alt_option_name) )
407
548
add_action( 'widgets_init', array( $this, '_register_widgets' ), 100 );
410
public function register($widget_class) {
552
* Register a widget subclass.
557
* @param string $widget_class The name of a {@see WP_Widget} subclass.
559
public function register( $widget_class ) {
411
560
$this->widgets[$widget_class] = new $widget_class();
414
public function unregister($widget_class) {
564
* Un-register a widget subclass.
569
* @param string $widget_class The name of a {@see WP_Widget} subclass.
571
public function unregister( $widget_class ) {
415
572
if ( isset($this->widgets[$widget_class]) )
416
573
unset($this->widgets[$widget_class]);
577
* Utility method for adding widgets to the registered widgets global.
419
582
public function _register_widgets() {
420
583
global $wp_registered_widgets;
421
584
$keys = array_keys($this->widgets);
544
707
* If you wanted to quickly create multiple sidebars for a theme or internally.
545
708
* This function will allow you to do so. If you don't pass the 'name' and/or
546
* 'id' in $args, then they will be built for you.
548
* The default for the name is "Sidebar #", with '#' being replaced with the
549
* number the sidebar is currently when greater than one. If first sidebar, the
550
* name will be just "Sidebar". The default for id is "sidebar-" followed by the
551
* number the sidebar creation is currently at. If the id is provided, and multiple
552
* sidebars are being defined, the id will have "-2" appended, and so on.
709
* 'id' in `$args`, then they will be built for you.
556
713
* @see register_sidebar() The second parameter is documented by register_sidebar() and is the same here.
557
* @uses parse_str() Converts a string to an array to be used in the rest of the function.
558
* @uses register_sidebar() Sends single sidebar information [name, id] to this
559
* function to handle building the sidebar.
561
* @param int $number Number of sidebars to create.
562
* @param string|array $args Builds Sidebar based off of 'name' and 'id' values.
715
* @param int $number Optional. Number of sidebars to create. Default 1.
716
* @param array|string $args {
717
* Optional. Array or string of arguments for building a sidebar.
719
* @type string $id The base string of the unique identifier for each sidebar. If provided, and multiple
720
* sidebars are being defined, the id will have "-2" appended, and so on.
721
* Default 'sidebar-' followed by the number the sidebar creation is currently at.
722
* @type string $name The name or title for the sidebars displayed in the admin dashboard. If registering
723
* more than one sidebar, include '%d' in the string as a placeholder for the uniquely
724
* assigned number for each sidebar.
725
* Default 'Sidebar' for the first sidebar, otherwise 'Sidebar %d'.
564
function register_sidebars($number = 1, $args = array()) {
728
function register_sidebars( $number = 1, $args = array() ) {
565
729
global $wp_registered_sidebars;
566
730
$number = (int) $number;
608
772
* If theme support for 'widgets' has not yet been added when this function is
609
773
* called, it will be automatically enabled through the use of add_theme_support()
611
* Arguments passed as a string should be separated by '&':
613
* e.g. 'name=Sidebar&id=my_prefix_sidebar'
615
* The same arguments passed as an array:
618
* 'name' => 'Sidebar',
619
* 'id' => 'my_prefix_sidebar',
623
* name - The name or title of the sidebar displayed in the admin dashboard.
624
* id - The unique identifier by which the sidebar will be called.
625
* before_widget - HTML content that will be prepended to each widget's HTML output
626
* when assigned to this sidebar.
627
* after_widget - HTML content that will be appended to each widget's HTML output
628
* when assigned to this sidebar.
629
* before_title - HTML content that will be prepended to the sidebar title when displayed.
630
* after_title - HTML content that will be appended to the sidebar title when displayed.
633
* @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
634
* @uses add_theme_support() to ensure widget support has been added.
636
* @param string|array $args Arguments for the sidebar being registered.
777
* @global array $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
779
* @param array|string $args {
780
* Optional. Array or string of arguments for the sidebar being registered.
782
* @type string $name The name or title of the sidebar displayed in the Widgets
783
* interface. Default 'Sidebar $instance'.
784
* @type string $id The unique identifier by which the sidebar will be called.
785
* Default 'sidebar-$instance'.
786
* @type string $description Description of the sidebar, displayed in the Widgets interface.
787
* Default empty string.
788
* @type string $class Extra CSS class to assign to the sidebar in the Widgets interface.
790
* @type string $before_widget HTML content to prepend to each widget's HTML output when
791
* assigned to this sidebar. Default is an opening list item element.
792
* @type string $after_widget HTML content to append to each widget's HTML output when
793
* assigned to this sidebar. Default is a closing list item element.
794
* @type string $before_title HTML content to prepend to the sidebar title when displayed.
795
* Default is an opening h2 element.
796
* @type string $after_title HTML content to append to the sidebar title when displayed.
797
* Default is a closing h2 element.
637
799
* @return string Sidebar ID added to $wp_registered_sidebars global.
639
801
function register_sidebar($args = array()) {
690
* Register widget for use in sidebars.
852
* Register an instance of a widget.
692
854
* The default widget option is 'classname' that can be overridden.
694
* The function can also be used to un-register widgets when $output_callback
856
* The function can also be used to un-register widgets when `$output_callback`
695
857
* parameter is an empty string.
699
* @uses $wp_registered_widgets Uses stored registered widgets.
700
* @uses $wp_register_widget_defaults Retrieves widget defaults.
702
* @param int|string $id Widget ID.
703
* @param string $name Widget display title.
704
* @param callback $output_callback Run when widget is called.
705
* @param array|string $options Optional. Widget Options.
706
* @param mixed $params,... Widget parameters to add to widget.
707
* @return null Will return if $output_callback is empty after removing widget.
861
* @global array $wp_registered_widgets Uses stored registered widgets.
862
* @global array $wp_register_widget_defaults Retrieves widget defaults.
864
* @param int|string $id Widget ID.
865
* @param string $name Widget display title.
866
* @param callback $output_callback Run when widget is called.
867
* @param array $options {
868
* Optional. An array of supplementary widget options for the instance.
870
* @type string $classname Class name for the widget's HTML container. Default is a shortened
871
* version of the output callback name.
872
* @type string $description Widget description for display in the widget administration
873
* panel and/or theme.
875
* @return null Will return if `$output_callback` is empty after removing widget.
709
function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) {
877
function wp_register_sidebar_widget( $id, $name, $output_callback, $options = array() ) {
710
878
global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks;
712
880
$id = strtolower($id);
824
992
* control form, but try hard to use the default width. The 'id_base' is for
825
993
* multi-widgets (widgets which allow multiple instances such as the text
826
994
* widget), an id_base must be provided. The widget id will end up looking like
827
* {$id_base}-{$unique_number}.
995
* `{$id_base}-{$unique_number}`.
831
* @param int|string $id Sidebar ID.
832
* @param string $name Sidebar display name.
833
* @param callback $control_callback Run when sidebar is displayed.
834
* @param array|string $options Optional. Widget options. See above long description.
835
* @param mixed $params,... Optional. Additional parameters to add to widget.
999
* @todo Document `$options` as a hash notation, re: WP_Widget::__construct() cross-reference.
1000
* @todo `$params` parameter?
1002
* @param int|string $id Sidebar ID.
1003
* @param string $name Sidebar display name.
1004
* @param callback $control_callback Run when sidebar is displayed.
1005
* @param array|string $options Optional. Widget options. See description above. Default empty array.
837
function wp_register_widget_control($id, $name, $control_callback, $options = array()) {
1007
function wp_register_widget_control( $id, $name, $control_callback, $options = array() ) {
838
1008
global $wp_registered_widget_controls, $wp_registered_widget_updates, $wp_registered_widgets, $_wp_deprecated_widgets_callbacks;
840
1010
$id = strtolower($id);
1348
* @param string $widget the widget's PHP class name (see default-widgets.php)
1349
* @param array $instance the widget's instance settings
1350
* @param array $args the widget's sidebar args
1353
function the_widget($widget, $instance = array(), $args = array()) {
1536
* @param string $widget The widget's PHP class name (see default-widgets.php).
1537
* @param array $instance Optional. The widget's instance settings. Default empty array.
1538
* @param array $args {
1539
* Optional. Array of arguments to configure the display of the widget.
1541
* @type string $before_widget HTML content that will be prepended to the widget's HTML output.
1542
* Default `<div class="widget %s">`, where `%s` is the widget's class name.
1543
* @type string $after_widget HTML content that will be appended to the widget's HTML output.
1545
* @type string $before_title HTML content that will be prepended to the widget's title when displayed.
1546
* Default `<h2 class="widgettitle">`.
1547
* @type string $after_title HTML content that will be appended to the widget's title when displayed.
1551
function the_widget( $widget, $instance = array(), $args = array() ) {
1354
1552
global $wp_widget_factory;
1356
1554
$widget_obj = $wp_widget_factory->widgets[$widget];