@@ -14,13 +14,175 @@ composer require alleyinteractive/wp-widget-control
1414
1515## Usage
1616
17- Use this package like so:
17+ WP Widget Control lets you programmatically manage WordPress sidebars and
18+ widgets. This can be useful for setting up default widget configurations,
19+ managing widget curation with code for end to end testing, or simply
20+ maintaining widget state in a more structured way.
21+
22+ When you have to manage widgets with code, WordPress doesn't provide a great way
23+ to do that. WP Widget Control fills this gap by providing a simple API for
24+ managing widgets programmatically.
25+
26+ Here are some common usage patterns:
27+
28+ ### Retrieve a Sidebar
29+
30+ ``` php
31+ use Alley\WP\Widget_Control\Sidebar;
32+
33+ $sidebar = Sidebar::from( 'sidebar-1' );
34+ ```
35+
36+ ### Append a Widget to a Sidebar
37+
38+ ``` php
39+ use Alley\WP\Widget_Control\Sidebar;
40+ use Alley\WP\Widget_Control\Widget;
41+
42+ $sidebar = Sidebar::from( 'sidebar-1' );
43+
44+ // Append a widget by its ID:
45+ $sidebar->append( 'example_widget-4' );
46+
47+ // You can create a new widget instance from the base ID and append it to the sidebar:
48+ $sidebar->append(
49+ Widget::from( 'example_widget' )->append( [ 'content' => 'Hello, World!' ] ),
50+ );
51+
52+ // Or by referencing the widget's class:
53+ $sidebar->append(
54+ Widget::from( \My\Custom\ExampleWidget::class )->append( [ 'content' => 'Hello, World!' ] ),
55+ );
56+
57+ // Save the sidebar to persist changes.
58+ $sidebar->save();
59+ ```
60+
61+ ### Insert Widgets Before or After Another Widget
62+
63+ ``` php
64+ use Alley\WP\Widget_Control\Sidebar;
65+ use Alley\WP\Widget_Control\Widget;
66+
67+ $sidebar = Sidebar::from( 'sidebar-1' );
68+
69+ // Insert a widget "block-99" before "block-2":
70+ $sidebar->insert_before( widget: 'block-99', before_widget_id: 'block-2' );
71+
72+ // Insert a widget "example_widget-6" after "example_widget-2":
73+ $sidebar->insert_after( widget: 'example_widget-6', after_widget_id: 'example_widget-2' );
74+
75+ // Also supports inserting a widget instance directly.
76+ // Inside a new widget instance before "example_widget-2":
77+ $sidebar->insert_before(
78+ widget: Widget::from( 'example_widget' )->append( [ 'content' => 'Hello, World!' ] ),
79+ before_widget_id: 'example_widget-2',
80+ );
81+
82+ // Save the sidebar to persist changes.
83+ $sidebar->save();
84+ ```
85+
86+ ### Remove a Widget by ID or Index
87+
88+ ``` php
89+ use Alley\WP\Widget_Control\Sidebar;
90+ use Alley\WP\Widget_Control\Widget_Instance;
91+
92+ $sidebar = Sidebar::from( 'sidebar-1' );
93+
94+ // Remove a widget by its ID:
95+ $sidebar->remove( 'block-2' );
96+
97+ // Remove a widget by its index:
98+ $sidebar->remove_index( 2 );
99+
100+ // Save the sidebar to persist changes.
101+ $sidebar->save();
102+ ```
103+
104+ ### Set All Widgets in a Sidebar
105+
106+ ``` php
107+ use Alley\WP\Widget_Control\Sidebar;
108+ use Alley\WP\Widget_Control\Widget;
109+
110+ $sidebar = Sidebar::from( 'sidebar-1' );
111+
112+ $sidebar->set( [
113+ // Use existing widget instances (they follow a widget_base-ID pattern).
114+ 'nav_menu-1',
115+ 'block-2',
116+ 'example_widget-2',
117+
118+ // You can also create a new widget instance and append it to the sidebar.
119+ Widget::from( 'example_widget' )->append( [ 'content' => 'Hello, World!' ] ),
120+ Widget::from( \My\Custom\ExampleWidget::class )->append( [ 'content' => 'Hello, World!' ] ),
121+ ] );
122+
123+ // Save the sidebar to persist changes.
124+ $sidebar->save();
125+ ```
126+
127+ ### Filter Widgets in a Sidebar
128+
129+ Remove a specific widget from a sidebar while keeping others:
18130
19131``` php
20- $package = Create_PHP_Package\WP_Widget_Control\WP_Widget_Control();
21- $package->perform_magic();
132+ use Alley\WP\Widget_Control\Sidebar;
133+ use Alley\WP\Widget_Control\Widget;
134+ use Alley\WP\Widget_Control\Widget_Instance;
135+
136+ $sidebar = Sidebar::from( 'sidebar-1' );
137+
138+ // Remove all widgets whose ID contains 'example_widget'.
139+ $sidebar->filter_by_id( function( string $widget_id ) {
140+ return ! str_contains( $widget_id, 'example_widget' );
141+ } );
142+
143+ // Keep only widgets of a certain type (using Widget_Instance).
144+ $sidebar->filter( function( Widget_Instance $widget ) {
145+ return $widget->id_base === 'example_widget';
146+ } );
147+
148+ // Save the sidebar to persist changes.
149+ $sidebar->save();
22150```
23151
152+ ### Clear All Widgets from a Sidebar
153+
154+ ``` php
155+ use Alley\WP\Widget_Control\Sidebar;
156+
157+ $sidebar = Sidebar::from( 'sidebar-1' );
158+
159+ $sidebar->clear();
160+
161+ // Save the sidebar to persist changes.
162+ $sidebar->save();
163+ ```
164+
165+ ### Full Sidebar Curation Example
166+
167+ In this example, we will set the sidebar to contain an instance of
168+ ` ExampleWidget ` and another instance of the ` block ` widget:
169+
170+ ``` php
171+ use Alley\WP\Widget_Control\Sidebar;
172+ use Alley\WP\Widget_Control\Widget;
173+ use Alley\WP\Widget_Control\Tests\ExampleWidget;
174+
175+ $sidebar->set( [
176+ Widget::from( ExampleWidget::class )->append( [ 'content' => 'Hello, World! 1' ] ),
177+ Widget::from( 'block' )->append( [ 'content' => 'Hello, World! 2' ] ),
178+ ] );
179+
180+ // Save the sidebar to persist changes.
181+ $sidebar->save();
182+ ```
183+
184+ These will be the only two widgets in the sidebar.
185+
24186## Changelog
25187
26188Please see [ CHANGELOG] ( CHANGELOG.md ) for more information on what has changed recently.
@@ -31,9 +193,9 @@ This project is actively maintained by [Alley
31193Interactive] ( https://github.com/alleyinteractive ) . Like what you see? [ Come work
32194with us] ( https://alley.co/careers/ ) .
33195
34- - [ Sean Fisher] (https://github.com/Sean Fisher )
196+ - [ Sean Fisher] ( https://github.com/srtfisher )
35197- [ All Contributors] ( ../../contributors )
36198
37199## License
38200
39- The GNU General Public License (GPL) license. Please see [ License File] ( LICENSE ) for more information.
201+ The GNU General Public License (GPL) license. Please see [ License File] ( LICENSE ) for more information.
0 commit comments