Starting From the Hello World Widget
Our Hello World Widget is a working widget that you can copy and turn into your own. It's in the extend-widgets-bundle plugin of our so-dev-examples Git repository.
To set up the example plugin:
- Install and activate the SiteOrigin Widgets Bundle.
- Clone the so-dev-examples repository, and copy its
extend-widgets-bundlefolder to your site'swp-content/pluginsfolder. - Activate the
extend-widgets-bundleplugin at Plugins.
The plugin's hello-world-widget folder holds the Hello World Widget. To turn a copy into your own widget:
- Choose an ID for your widget, such as
my-awesome-widget. - Copy the
hello-world-widgetfolder, and rename the copy and thehello-world-widget.phpfile inside it with your ID, such asmy-awesome-widgetandmy-awesome-widget.php. The folder and the file must have the same name. - Open the PHP file and rename the
Hello_World_Widgetclass, for example toMy_Awesome_Widget. - In the class constructor, the first argument to the parent constructor is the widget ID. Replace
hello-world-widgetwith your ID. - At the bottom of the file, the widget is registered. Replace
hello-world-widgetwith your ID andHello_World_Widgetwith your class name. - In the metadata header above the class, change the
Widget Namefield to your widget's name, such asWidget Name: My Awesome Widget. The name can be anything, but the Widgets Bundle lists the widget only if the header has aWidget Namefield. - Go to Plugins > SiteOrigin Widgets and activate your widget. New widgets start inactive.
Adding a Separate Widgets Folder
The siteorigin_widgets_widget_folders filter registers a folder of your own widgets, so you can keep them apart from the SiteOrigin widgets:
<?php
function add_my_awesome_widgets_collection( $folders ) {
$folders[] = plugin_dir_path( __FILE__ ) . 'extra-widgets/';
return $folders;
}
add_filter( 'siteorigin_widgets_widget_folders', 'add_my_awesome_widgets_collection' );The Widgets Bundle looks for PHP files in each subfolder of a registered folder. It lists every file whose metadata header has a Widget Name field as a widget that users can activate and use in widget areas, Page Builder and the Block Editor.
The extend-widgets-bundle example plugin is a standard WordPress plugin that uses this filter to add its extra-widgets folder.