How to create custom sidebar in wordpress without plugin

First, you need to register the custom sidebar in your theme. To do this, you will add the following code to your theme’s functions.php file. If you’re using a child theme, add it to the functions.php file of your child theme to ensure that changes are not lost during theme updates.

1. Open your functions.php file located in the root folder of your theme or child theme.
2. Add the following code to register the sidebar:


  // Register the custom sidebar
function custom_sidebar() {
    register_sidebar(
        array(
            'name'          => 'Custom Sidebar', 
            'id'            => 'custom_sidebar',  
            'before_widget' => '
', 'after_widget' => '
', ) ); } add_action( 'widgets_init', 'custom_sidebar' );

Explanation of Code:

-> name: The name of the sidebar that will appear in the WordPress admin area.
-> id: The unique ID used to reference the sidebar in your theme.
-> before_widget / after_widget: HTML elements wrapped around the widget when displayed.
-> before_title / after_title: HTML elements wrapped around the widget title.

This code registers a sidebar with the name “Custom Sidebar” and the ID “custom_sidebar”. You can change the name and id values to whatever you prefer.

Step 2: Display the Sidebar in Your Theme
Now that you’ve registered the sidebar, you need to display it in your theme files. Typically, sidebars are added in files like sidebar.php or within a specific page template like page.php or single.php.

1. Locate or create the sidebar: If your theme has a sidebar.php file, open it. If it doesn’t, you can add the following code to any template file where you want the sidebar to appear (e.g., page.php or single.php).

2. Insert the following code to display the sidebar:


if ( is_active_sidebar( 'custom_sidebar' ) ) {
    echo '';
}

Explanation of Code:

-> is_active_sidebar(): This function checks if there are any widgets in the specified sidebar. If there are, it returns true.
-> dynamic_sidebar(): This function outputs the content (widgets) added to the sidebar in the WordPress admin.

Step 3: Add Widgets to Your Sidebar

After registering the sidebar and displaying it in your theme, you can now add widgets to the sidebar through the WordPress admin interface.

1. Go to Appearance > Widgets.
2. You should see your custom sidebar (“Custom Sidebar”) listed there.
3. Drag and drop widgets into the sidebar as desired.

Leave a Comment

Scroll to Top