Register a WordPress Sidebar for Oxygen Builder

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 on widgets_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_title and after_title set the HTML around each widget and its title. %1$s becomes the widget ID and %2$s its 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 in if ( is_active_sidebar( 'primary-sidebar' ) ) to skip empty areas.

Customise it

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 widget and widget-title classes 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.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You’ll also get DPlugins product updates. No spam, and you can unsubscribe anytime.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You'll also get DPlugins product updates. No spam, and you can unsubscribe anytime.