PHP Class Widget_Command, wp-cli

## EXAMPLES # List widgets on a given sidebar $ wp widget list sidebar-1 +----------+------------+----------+----------------------+ | name | id | position | options | +----------+------------+----------+----------------------+ | meta | meta-6 | 1 | {"title":"Meta"} | | calendar | calendar-2 | 2 | {"title":"Calendar"} | +----------+------------+----------+----------------------+ # Add a calendar widget to the second position on the sidebar $ wp widget add calendar sidebar-1 2 Success: Added widget to sidebar. # Update option(s) associated with a given widget $ wp widget update calendar-1 --title="Calendar" Success: Widget updated. # Delete one or more widgets entirely $ wp widget delete calendar-2 archive-1 Success: 2 widgets removed from sidebar.
Inheritance: extends WP_CLI_Command
Show file Open project: wp-cli/wp-cli

Public Methods

Method Description
add ( $args, $assoc_args ) Add a widget to a sidebar.
deactivate ( $args, $assoc_args ) Deactivate one or more widgets from an active sidebar.
delete ( $args, $assoc_args ) Delete one or more widgets from a sidebar.
list_ ( $args, $assoc_args ) List widgets associated with a sidebar.
move ( $args, $assoc_args ) Move the position of a widget.
reset ( $args, $assoc_args ) Reset sidebar.
update ( $args, $assoc_args ) Update options for an existing widget.

Private Methods

Method Description
get_sidebar_widgets ( string $sidebar_id ) : array Get the widgets (and their associated data) for a given sidebar
get_widget_data ( string $widget_id ) : array Get the widget's name, option index, sidebar, and sidebar index from its ID
get_widget_obj ( string $id_base ) : WP_Widget | false Get a widget's instantiated object based on its name
get_widget_options ( string $name ) : array Get the options for a given widget
move_sidebar_widget ( string $widget_id, string | null $current_sidebar_id, string $new_sidebar_id, integer | null $current_index, integer $new_index ) Reposition a widget within a sidebar or move to another sidebar.
sanitize_widget_options ( string $id_base, mixed $dirty_options, mixed $old_options ) : mixed Clean up a widget's options based on its update callback
update_widget_options ( string $name, $value ) Update the options for a given widget
validate_sidebar ( string $sidebar_id ) Check whether a sidebar is a valid sidebar
validate_sidebar_widget ( string $widget_id ) Check whether the specified widget is on the sidebar
wp_get_sidebars_widgets ( ) Re-implementation of wp_get_sidebars_widgets() because the original has a nasty global component

Method Details

add() public method

Creates a new widget entry in the database, and associates it with the sidebar. ## OPTIONS : Widget name. : ID for the corresponding sidebar. [] : Widget's current position within the sidebar. Defaults to last [--=] : Widget option to add, with its new value ## EXAMPLES # Add a new calendar widget to sidebar-1 with title "Calendar" $ wp widget add calendar sidebar-1 2 --title="Calendar" Success: Added widget to sidebar.
public add ( $args, $assoc_args )

deactivate() public method

Moves widgets to Inactive Widgets. ## OPTIONS ... : Unique ID for the widget(s) ## EXAMPLES # Deactivate the recent-comments-2 widget. $ wp widget deactivate recent-comments-2 Success: 1 widget deactivated.
public deactivate ( $args, $assoc_args )

delete() public method

## OPTIONS ... : Unique ID for the widget(s) ## EXAMPLES # Delete the recent-comments-2 widget from its sidebar. $ wp widget delete recent-comments-2 Success: Deleted 1 of 1 widgets.
public delete ( $args, $assoc_args )

list_() public method

## OPTIONS : ID for the corresponding sidebar. [--fields=] : Limit the output to specific object fields. [--format=] : Render output in a particular format. --- default: table options: - table - csv - ids - json - count - yaml --- ## AVAILABLE FIELDS These fields will be displayed by default for each widget: * name * id * position * options There are no optionally available fields. ## EXAMPLES $ wp widget list sidebar-1 --fields=name,id --format=csv name,id meta,meta-5 search,search-3
public list_ ( $args, $assoc_args )

move() public method

Changes the order of a widget in its existing sidebar, or moves it to a new sidebar. ## OPTIONS : Unique ID for the widget [--position=] : Assign the widget to a new position. [--sidebar-id=] : Assign the widget to a new sidebar ## EXAMPLES # Change position of widget $ wp widget move recent-comments-2 --position=2 Success: Widget moved. # Move widget to Inactive Widgets $ wp widget move recent-comments-2 --sidebar-id=wp_inactive_widgets Success: Widget moved.
public move ( $args, $assoc_args )

reset() public method

Removes all widgets from the sidebar and places them in Inactive Widgets. ## OPTIONS [...] : One or more sidebars to reset. [--all] : If set, all sidebars will be reset. ## EXAMPLES # Reset a sidebar $ wp widget reset sidebar-1 Success: Sidebar 'sidebar-1' reset. # Reset multiple sidebars $ wp widget reset sidebar-1 sidebar-2 Success: Sidebar 'sidebar-1' reset. Success: Sidebar 'sidebar-2' reset. # Reset all sidebars $ wp widget reset --all Success: Sidebar 'sidebar-1' reset. Success: Sidebar 'sidebar-2' reset. Success: Sidebar 'sidebar-3' reset.
public reset ( $args, $assoc_args )

update() public method

## OPTIONS : Unique ID for the widget [--=] : Field to update, with its new value ## EXAMPLES # Change calendar-1 widget title to "Our Calendar" $ wp widget update calendar-1 --title="Our Calendar" Success: Widget updated.
public update ( $args, $assoc_args )