Block Editor FAQ

How do I add a Block Area to my site with code?

All that is really needed is a call to the sixtenpressblockeditor_area()->show() function, with a required parameter of either the block area ID or slug. With an ID:

sixtenpressblockeditor_area()->show( 14 );

Here’s an example of how you could set up default block areas in your code:

add_action( 'init', 'leaven_maybe_run_block_editor_areas' );
/**
* Check if the block area plugin is active.
* If so, remove the Primary Sidebar, then the Genesis code to output it,
* and replace it with the 'primary' block area.
*/
function leaven_maybe_run_block_editor_areas() {
	if ( ! function_exists( 'sixtenpressblockeditor_area' ) ) {
		return;
	}
	// Unregister the primary sidebar
	unregister_sidebar( 'sidebar' );

	// Remove the default primary sidebar output
	remove_action( 'genesis_sidebar', 'genesis_do_sidebar' );

	add_action( 'genesis_sidebar', 'leaven_primary_block_area' );
}

/**
* Replace the primary sidebar output with the primary block area if it exists.
*/
function leaven_primary_block_area() {
	if ( leaven_check_block_editor_setting( 'genesis_sidebar' ) ) {
		return;
	}
	sixtenpressblockeditor_area()->show( 'primary' );
}
/**
* Helper function to get the block editor plugin setting and see if a
* block area has already been created/assigned to this hook.
* This allows the block areas which have been added via code to be
* overridden or replaced with block areas added on the plugin settings page.
*
* @param $hook
* @return bool
*/
function leaven_check_block_editor_setting( $hook ) {
	$setting = sixtenpressblockeditor_get_setting( 'block_areas' );

	return in_array( $hook, array_column( $setting, 'hook' ), true );
}
Return to Six/Ten Press Block Editor