code for how to add sidebar in wordpress
1. Add this to your `functions.php`:
function my_custom_sidebar() {
register_sidebar( array(
'name' => __( 'Custom Sidebar', 'your-textdomain' ),
'id' => 'custom-sidebar',
'before_widget' => '
',
'after_widget' => '
',
'before_title' => '
'after_title' => '',
));
}
add_action( 'widgets_init', 'my_custom_sidebar' );
function sidebar_shortcode() {
ob_start();
if ( is_active_sidebar( 'custom-sidebar' ) ) {
dynamic_sidebar( 'custom-sidebar' );
} else {
echo '
No widgets found in sidebar.
';
}
return ob_get_clean();
}
add_shortcode( 'custom_sidebar', 'sidebar_shortcode' );
---
2. Create this file
Path: `wp-content/themes/twentytwentyfive/template-parts/sidebar-custom.php`
No widgets found in sidebar.
3. [custom_sidebar]
In your WordPress editor (block editor):
* Insert a Shortcode block
* Paste: [custom_sidebar]
If you’re using a column layout, place this in the left column.
4. Add Widgets
Go to:
Appearance > Widgets → Custom Sidebar → Add a Text block or any block
Save, and you’re done.