wordpress widgets disappear
While working recently on a project, I got introduced to the power of widgets, previously, I would have used special posts to allow people to manage content in awkward places on their websites, then it hit me that widgets could offer more flexibility. Having only got started, I hit a problem pretty quickly, everytime I had put my custom widget into place, it disappeared completely from the back-end, I didn’t change my theme or anything, just any time the page was refreshed the widget would vanish, but I figured out what the problem was. When you are making your theme ‘widget aware’ you have to put some special code into certain files in your theme, for example, this would go into your functions.php file:
if (function_exists('register_sidebar')) {
$social = array(
'name' => sprintf(__('Header Links'), $i),
'id' => 'sidebar-$i',
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>');
register_sidebar($social);
}
This is the preferred code to use when creating widgets, but anytime I used the widget it would disappear with no trace, that was until I made a few changes:
if (function_exists('register_sidebar')) {
$social = array(
'name' => 'bottom_left',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2>',
'after_title' => '</h2>');
register_sidebar($social);
}
Spot the difference? I stripped out the space in the ‘name’ property, and that was it.
Hopefully, this might save you some time!
Reader's Comments
Ahh! Yes! Thank you for sharing this! I’ve been going nuts trying to get to the bottom of this issue.
Thank you soooooo much!!! How simple a change to make such a difference! I was about to pull my hair out by deactivating plug-ins, re-writting my code, and everything.
Leave a Reply