Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Pavle Lucic
Pavle Lucic
10,801 Points

Call widget areas - confusing

In tutorial , calling for register_sidebar is like this

wpt_create_widget(//some arguments);

In wordpress documentation calling register_sidebar() is done by this method add_action( 'widgets_init', 'wpt_create_widget' );

How wordpress know that we want to create widget area by calling wpt_create_widget() ?

1 Answer

Hi Pavle :)

When creating the "wigetized areas" you first write a function in the functions.php to create that widget, like this below:

<?php 

//Add theme support for widgetized areas / create widget

function create_widget($name, $id, $description) {

    register_sidebar(array(
        'name' => __( $name ),   
        'id' => $id, 
        'description' => __( $description ),
        'before_widget' => '<div class="widget">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-header">',
        'after_title' => '</h3>'
    ));

}

// Create widgets 
create_widget("Front Page", "front-page", "Displays on the home page");

?>

Then you call that widget in your template files like this:

<div class="home-page-widget">

    <?php if( !dynamic_sidebar( 'front-page' ) ): ?>

        <p>Please Add Widget Front Page</p>

    <?php endif; ?>

</div>

The above PHP Block is where that widget will be displayed, you position the above code in the template file i.e. front-page.php where you want the widgetized area to be placed.

Then by some WordPress magic from the back end in your admin you should be all good to go and ad the widget content :)

Hope this helps Craig :)

Pavle Lucic
Pavle Lucic
10,801 Points

Hi Craig Watson , tnx on answer.

But the point is, i could not find in wordpress codex create_widget() function ?

Hi Pavle,

My apologies for getting the wrong end of the stick with your question.

The create_widget() function is a function we have created to make our own widgets based on the params in the register sidebar array.

All we are doing is calling our own function and passing values based on the register sidebar array.

create_widget() is not a default WP Function just one we have created :)

Craig

Pavle Lucic
Pavle Lucic
10,801 Points

Hey Craig Watson , no problem at all.

In codex, calling widget is done by add_action( 'widgets_init', 'wpt_create_widget' ); ?

That is confusing to me?

I am starting to wonder if there have been some changes in the way this can be done as this is the snippet from codex that seems to refer to a similar outcome:

<?php

add_action( 'widgets_init', 'theme_slug_widgets_init' );
function theme_slug_widgets_init() {
    register_sidebar( array(
        'name' => __( 'Main Sidebar', 'theme-slug' ),
        'id' => 'sidebar-1',
        'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ),
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget'  => '</li>',
    'before_title'  => '<h2 class="widgettitle">',
    'after_title'   => '</h2>',
    ) );
}

?>

The only thing I don't seem to be ale to find in relation to the above is how to create your separate widgets from there. The way Zac does it is the way I have been doing it and have not had any issues so far.

Sorry I cant be of much more help on this Pavle :)

Craig

Pavle Lucic
Pavle Lucic
10,801 Points

Maybe Zac Gordon can help us to figure out?