Adding custom widgets

Hello! I’m having a small problem that used to be a no-brainer with roots.

In the lib/ folder there used to be a widgets.php where one could add custom widgets :smile:

<?php
/**
 * Register sidebars and widgets
 */
function roots_widgets_init() {
  // Sidebars
  register_sidebar(array(
'name'          => __('Primary', 'roots'),
'id'            => 'sidebar-primary',
'before_widget' => '<section class="widget %1$s %2$s"><div class="widget-inner">',
'after_widget'  => '</div></section>',
'before_title'  => '<h3>',
'after_title'   => '</h3>',
 ));
    // Widgets
    register_widget('Roots_Vcard_Widget');
    }
add_action('widgets_init', 'roots_widgets_init');  
}

The latest Roots doesn’t have any such php file in lib and when I tried to copy the code into sidebar.php evidently that crashed the whole Wordpress theme because roots_widgets_init isn’t being used by anything.

Any tips on how to fix this mess? I checked this issue but the instructions were too vague for me to work with. I tried making a new file called widgets.php and add the following 'lib/widgets.php', to functions.php but that also crashed the theme. Help?

U should init widget in init.php file now

I don’t think that’s what’s going on. In the code you pasted, roots_widgets_init is being used with widgets_init.

What specific errors did you see? “crashed the whole Wordpress theme” doesn’t give us enough details.

In the code you pasted you’re registering a widget called Roots_Vcard_Widget. Does that exist in your code?

Adding custom widgets with Roots is no different than adding custom widgets with any other theme, or plugin, in WordPress. You don’t need to include older Roots code into a newer Roots version in order to add a custom widget. There’s lots of resources out there that show you how to create custom widgets.

Benword, by crash the Wordpress I mean:

The wordpress install becomes blank - as if php include() was hanging forever - and wp-admin is blank too - the only way to fix it is to ssh into the install and delete the broken wordpress theme from the command line and then the wordpress install displays: “the current theme is broken, reverting to default theme”

This isn’t related to anything in a default Roots installation. Look at error logs and double check the code that you’re trying to add.

Also, use http://codex.wordpress.org/Widgets_API and other resources for information on adding a custom widget. Like I said in my previous post, adding custom widgets with Roots is no different than adding custom widgets with any other theme, or plugin, in WordPress.

1 Like

@gersande, make sure you set define('WP_DEBUG', true); in your wp-config.php file to output PHP errors instead of a white screen.

My guess is that you aren’t namespacing correctly, try using add_action('widgets_init', __NAMESPACE__ . '\\roots_widgets_init'); as your widget add_action in init.php.

move add_action('widgets_init', 'roots_widgets_init'); outside your function block. It looks like a recursive for me.