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

Troubleshooting--Widgets Dilemma

In our last thrilling episode, I was confused about the difference between the register_widget() function & register_sidebar(). That's sorted. But here's a related question.

When troubleshooting via Theme Check, I get this error message:

"Sidebars need to be registered in a custom function hooked to the widgets_init action. See: register_sidebar()."

Yet I have the following code in the functions.php file & I have widget areas in the WP admin area:

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

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

}

wpt_create_widget( 'Page Sidebar', 'page', 'Displays on the side of pages with a sidebar' );
wpt_create_widget( 'Blog Sidebar', 'blog', 'Displays on the side of pages in the blog section' );

What am I missing? Should I have the function above located elsewhere as well? Does this pertain to a specific template?

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

I'm not an expert but I believe if your widget is showing up in the admin area and works on the front end you can safely ignore this message.

I do wonder about the way you;ve set up some of your key values

'name' => __( $name )

Is that the correct format or would

'name' => $name

work better for you?

Good batch! Here's what the codex has to say about it:

Usage <?php $translated_text = __( $text, $domain ); ?>

Parameters $text (string) (required) Text to translate Default: None $domain (string) (optional) Domain to retrieve the translated text Default: 'default' Return Values (string) Translated text

Examples Make a string inside your plugin or theme translatable:

$translated = __( 'Hello World!', 'mytextdomain' );

'mytextdomain' needs to be a unique text domain used throughout your plugin/theme. This should always be directly passed as a string literal as shown above, not a string assigned to a variable or constant. E.g., this is incorrect:

$text_domain = 'mytextdomain'; $string = 'Hello World!'; $translated = __( $string, $text_domain );

This seems to work, but it will interfere in automatic parsing of your plugin/theme's files for translation.

         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>',
    ) );