Core base class extended to register widgets.

This class must be extended for each widget, and WP_Widget::widget() must be overridden.

If adding widget options, WP_Widget::update() and WP_Widget::form() should also be overridden.

package Akismet
inherited_from \WP_Widget

 Methods

PHP4 constructor.

WP_Widget(string $id_base, string $name, array $widget_options = array(), array $control_options = array()
Inherited
since 2.8.0
see

Parameters

$id_base

string

Optional Base ID for the widget, lowercase and unique. If left empty, a portion of the widget's class name will be used Has to be unique.

$name

string

Name for the widget displayed on the configuration page.

$widget_options

array

Optional. Widget options. See wp_register_sidebar_widget() for information on accepted arguments. Default empty array.

$control_options

array

Optional. Widget control options. See wp_register_widget_control() for information on accepted arguments. Default empty array.

PHP5 constructor.

__construct() 
inherited_from \WP_Widget::__construct()

PHP5 constructor.

__construct(string $id_base, string $name, array $widget_options = array(), array $control_options = array()
Inherited
since 2.8.0

Parameters

$id_base

string

Optional Base ID for the widget, lowercase and unique. If left empty, a portion of the widget's class name will be used Has to be unique.

$name

string

Name for the widget displayed on the configuration page.

$widget_options

array

Optional. Widget options. See wp_register_sidebar_widget() for information on accepted arguments. Default empty array.

$control_options

array

Optional. Widget control options. See wp_register_widget_control() for information on accepted arguments. Default empty array.

Retrieves the widget display callback.

_get_display_callback() : callable
Inherited
since 2.8.0

Returns

callableDisplay callback.

Retrieves the form callback.

_get_form_callback() : callable
Inherited
since 2.8.0

Returns

callableForm callback.

Retrieves the widget update callback.

_get_update_callback() : callable
Inherited
since 2.8.0

Returns

callableUpdate callback.

Register all widget instances of this widget class.

_register() 
Inherited
since 2.8.0

Registers an instance of the widget class.

_register_one(integer $number = -1
Inherited
since 2.8.0

Parameters

$number

integer

Optional. The unique order number of this widget instance compared to other instances of the same class. Default -1.

Sets the internal order number for the widget instance.

_set(integer $number) 
Inherited
since 2.8.0

Parameters

$number

integer

The unique order number of this widget instance compared to other instances of the same class.

css()

css() 

Generates the actual widget content (Do NOT override).

display_callback(array $args, integer|array $widget_args = 1
Inherited

Finds the instance and calls WP_Widget::widget().

since 2.8.0

Parameters

$args

array

Display arguments. See WP_Widget::widget() for information on accepted arguments.

$widget_args

integerarray

{ Optional. Internal order number of the widget instance, or array of multi-widget arguments. Default 1.

@type int $number Number increment used for multiples of the same widget.

}

Outputs the settings update form.

form(array $instance) 
inherited_from \WP_Widget::form()

Parameters

$instance

Outputs the settings update form.

form(array $instance) : string
Inherited
since 2.8.0

Parameters

$instance

array

Current settings.

Returns

stringDefault return is 'noform'.

Generates the widget control form (Do NOT override).

form_callback(integer|array $widget_args = 1) : string | null
Inherited
since 2.8.0

Parameters

$widget_args

integerarray

{ Optional. Internal order number of the widget instance, or array of multi-widget arguments. Default 1.

@type int $number Number increment used for multiples of the same widget.

}

Returns

stringnull

Constructs id attributes for use in WP_Widget::form() fields.

get_field_id(string $field_name) : string
Inherited

This function should be used in form() methods to create id attributes for fields to be saved by WP_Widget::update().

since 2.8.0
since 4.4.0 Array format field IDs are now accepted.

Parameters

$field_name

string

Field name.

Returns

stringID attribute for `$field_name`.

Constructs name attributes for use in form() fields

get_field_name(string $field_name) : string
Inherited

This function should be used in form() methods to create name attributes for fields to be saved by update()

since 2.8.0
since 4.4.0 Array format field names are now accepted.

Parameters

$field_name

string

Field name

Returns

stringName attribute for $field_name

Retrieves the settings for all instances of the widget class.

get_settings() : array
Inherited
since 2.8.0

Returns

arrayMulti-dimensional array of widget instance settings.

Determines whether the current request is inside the Customizer preview.

is_preview() : boolean
Inherited

If true -- the current request is inside the Customizer preview, then the object cache gets suspended and widgets should check this to decide whether they should store anything persistently to the object cache, to transients, or anywhere else.

since 3.9.0
global WP_Customize_Manager $wp_customize

Returns

booleanTrue if within the Customizer preview, false if not.

Saves the settings for all instances of the widget class.

save_settings(array $settings) 
Inherited
since 2.8.0

Parameters

$settings

array

Multi-dimensional array of widget instance settings.

Updates a particular instance of a widget.

update(array $new_instance, array $old_instance) 

This function should check that $new_instance is set correctly. The newly-calculated value of $instance should be returned. If false is returned, the instance won't be saved/updated.

inherited_from \WP_Widget::update()

Parameters

$new_instance

$old_instance

Updates a particular instance of a widget.

update(array $new_instance, array $old_instance) : array
Inherited

This function should check that $new_instance is set correctly. The newly-calculated value of $instance should be returned. If false is returned, the instance won't be saved/updated.

since 2.8.0

Parameters

$new_instance

array

New settings for this instance as input by the user via WP_Widget::form().

$old_instance

array

Old settings for this instance.

Returns

arraySettings to save or bool false to cancel saving.

Handles changed settings (Do NOT override).

update_callback(integer $deprecated = 1
Inherited
since 2.8.0
global array $wp_registered_widgets

Parameters

$deprecated

integer

Not used.

Echoes the widget content.

widget(array $args, array $instance) 

Sub-classes should over-ride this function to generate their widget code.

inherited_from \WP_Widget::widget()

Parameters

$args

$instance

Echoes the widget content.

widget(array $args, array $instance) 
Inherited

Sub-classes should over-ride this function to generate their widget code.

since 2.8.0

Parameters

$args

array

Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.

$instance

array

The settings for the particular instance of the widget.

 Properties

 

Alt option name for this widget type.

$alt_option_name : string

Default

since 2.8.0
 

Option array passed to wp_register_widget_control().

$control_options : array

Default

since 2.8.0
 

Unique ID string of the current instance (id_base-number).

$id : boolean | string

Default

false
since 2.8.0
 

Root ID for all widgets of this type.

$id_base : mixed | string

Default

since 2.8.0
 

Name for this widget type.

$name : string

Default

since 2.8.0
 

Unique ID number of the current instance.

$number : boolean | integer

Default

false
since 2.8.0
 

Option name for this widget type.

$option_name : string

Default

since 2.8.0
 

Whether the widget data has been updated.

$updated : boolean

Default

false

Set to true when the data is updated after a POST submit - ensures it does not happen twice.

since 2.8.0
 

Option array passed to wp_register_sidebar_widget().

$widget_options : array

Default

since 2.8.0