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

WP Widget not work properly.

Hello Treehouse Friends!

My problem: Imagine i create new widget in function.php
create_widget("iPhone Cases IMG", "iphone cases page img", "Link in iPhone Cases Page Img"); After this, in Appearance/Widget add new information: text, image, link. - Save this! In frond-end site show all this new information. If i go another part wordpress menu. Not important - any different place in WP-admin. And if i go back Appearance/Widget , try change information in "iPhone Cases IMG" widget ( Imagine change image ). When i open, this widget nothing shows. Just empty widget. But Frond-end wp site still show information what i add in firs time.

Maybe you know what mistake i have? Thanks!

Stanley Thijssen
Stanley Thijssen
22,831 Points

Can you post your code of how you created your widget? Seems like your widget does'nt save how it suppose to.

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>',
    'after_title' => '</h3>'
));

}

// Widget Section =========================================================================

create_widget( 'Search field', 'front-top', 'Displays on the top of the homepage' );

create_widget('footer menu', 'footer custom menu', 'Link in Front Page');

create_widget("iPhone Cases", "iphone cases page", "Link in iPhone Cases Page"); create_widget("iPhone Cases IMG", "iphone cases page img", "Link in iPhone Cases Page Img");

create_widget("iPad Cases", "ipad cases page", "iPad Cases Page");

create_widget('iPod Cases', 'ipod cases page', 'iPod Cases Page');

create_widget("Bags", "bags pages", "Link in Bags Page");

You ask this code example?

4 Answers

Stanley Thijssen
Stanley Thijssen
22,831 Points

Can you try to add this code to the end of your code after you close your create_widget() function and try again?:

           add_action( 'init', 'create_widget' );

or

           add_action( 'widgets_init', 'create_widget' );

After add your code after 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>', 'after_title' => '</h3>' ));}

add_action( 'widgets_init', 'create_widget' );

or

add_action( 'init', 'create_widget' );

Have this massage in admin page: Warning: Missing argument 2 for create_widget() in /data01/virt49977/domeenid/www.istore.ee/htdocs/wp-content/themes/Bootstrap Teamplate/functions.php on line 149

Warning: Missing argument 3 for create_widget() in /data01/virt49977/domeenid/www.istore.ee/htdocs/wp-content/themes/Bootstrap Teamplate/functions.php on line 149

Stanley Thijssen
Stanley Thijssen
22,831 Points
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>',
    'after_title' => '</h3>'
));
} 
add_action( 'init', 'create_widget' ); 

So this is how your code looks like now?

Yes. in functions.php the same look. How properly post code in this forum. My code look like shit.)

Stanley Thijssen
Stanley Thijssen
22,831 Points

You can check the cheatsheet on the bottom of the page underneath the post box

Stanley Thijssen
Stanley Thijssen
22,831 Points
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>',
    'after_title' => '</h3>'
));
} 

is the right code. So your code was right. I was confused because you were using a custom function around your register_sidebar() function.

So your problem now is you change a name inside the create_widget() function u created and test it again in the admin panel it does'nt show the changes?

I'm delete all old widget. Turn off all plugin, after turn on. And widget area start work properly its some strange bug. Thanks fore helping! )