Oxygen Builder disables the WordPress theme, so a fresh Oxygen site has no sidebar and clients get no widget area to manage. This snippet registers a WordPress sidebar, so Appearance → Widgets shows a Primary Sidebar they can fill with recent posts, categories, menus or any block.
You design where the sidebar appears in Oxygen once; the client changes what is in it from the dashboard without touching the builder.
The snippet
<?php
/**
* Register a sidebar (widget area) that the client fills under Appearance → Widgets.
*/
add_action( 'widgets_init', 'dpc_register_sidebars' );
function dpc_register_sidebars() {
register_sidebar(
array(
'name' => __( 'Primary Sidebar', 'dpc' ),
'id' => 'primary-sidebar',
'description' => __( 'Widgets shown where the template outputs the Primary Sidebar.', 'dpc' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
)
);
// More areas: copy the register_sidebar() call and give each one its own id.
}
How it works
register_sidebar()adds a widget area onwidgets_init, the hook WordPress fires when it sets up widgets. Registering a sidebar also turns on widget support, so the Widgets screen appears even though Oxygen has no theme.before_widget,after_widget,before_titleandafter_titleset the HTML around each widget and its title.%1$sbecomes the widget ID and%2$sits CSS class.- The
id(primary-sidebar) is how templates and builders find the area. Keep it once widgets are placed, or they move to Inactive widgets.
The older version of this post used an unprefixed function name and a placeholder text domain.
Add it with Scripts Organizer
In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Everywhere, tick PHP under Script location, paste the code into the PHP editor and publish. Leave Action/Hook empty. Widgets are rendered on the front end and managed in wp-admin and the REST API, so the sidebar must be registered on every request.
The same code also works unchanged in a small plugin or an mu-plugin.
Show it on the site
- In Oxygen, open Add → WordPress, where registered sidebars are listed, and drop Primary Sidebar into the template.
- In a theme template, call
dynamic_sidebar( 'primary-sidebar' );, wrapped inif ( is_active_sidebar( 'primary-sidebar' ) )to skip empty areas.
Customise it
- More areas: copy the
register_sidebar()call and changenameandid, for example a Shop Sidebar withshop-sidebar. - Different title markup: change
before_titleandafter_title, or see Change WordPress Widget Title From h2 to h4. - Hide a widget title on some pages: see Remove Widget Title Conditionally.
Gotchas
- Since WordPress 5.8 the Widgets screen uses blocks. If clients prefer the old widget forms, see Restore Classic Widgets in WordPress Without a Plugin.
- Widget styling comes from your Oxygen design, not from a theme, so style the
widgetandwidget-titleclasses in Oxygen or your stylesheet. - An empty sidebar prints nothing, so leave room in the layout for that case.
In the Scripts Organizer library Import it with one click from Scripts Organizer.