WordPress dev – add widget to your theme

In order to add some widgets area to your theme we need to do 2 steps. At first we have to register those areas in the functions.php file, then we need to specify where this area should appear within our website.
In the example below, we register 2 widgets. Pay attention to the id key-value as this is the way we connect widget areas.

Functions php

function pikademia_register_widgets() {

    $shared_args = array(
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
        'before_widget' => '<div class="widget %2$s"><div class="widget-content">',
        'after_widget'  => '</div></div>',
    );
    register_sidebar(
        array_merge(
            $shared_args,
            array(
                'name'        => 'Widget #1',
                'id'          => 'sidebar-1',
                'description' => 'Desc'
            )
        )
    );
    register_sidebar(
        array_merge(
            $shared_args,
            array(
                'name'        => 'Widget #2',
                'id'          => 'sidebar-2',
                'description' => 'Desc2'
            )
        )
    );
}

add_action( 'widgets_init', 'pikademia_register_widgets' );

Inside website

<?php dynamic_sidebar('sidebar-1'); ?>
<?php dynamic_sidebar('sidebar-2'); ?>
Scroll to Top