I currently have a plugin (not a widget) that allows people to use shortcode to put the results on their web pages and posts. Some people have asked for a widget option, so the output can go in the sidebar (if the theme supports it).

Is this possible? I tried putting together a dummy widget (using code from the WP docs), but if I try to activate it like this:

 add_action( 'widgets_init', 'src_load_widgets' );
  function src_load_widgets() {


I just get this error:

Fatal error: Class 'Foo_Widget' not found in /Users/me/My Webs/wordpress/wp-includes/widgets.php on line 324

Currently, the widget script is in the same folder as my plugin script since I want to same admin screen as I do for my plugin. Not sure if this is the problem...so basically, I'm a bit lost. Thanks for any help you can provide here.

1 Answer 1


A widget is a PHP object that echoes string data to STDOUT when its widget() method is called. -- WordPress Widgets API

To create the widget you have to extend the WP_Widget class with your widget class. You use the class name in the register_widget function.

class Foo_Widget extends WP_Widget {
   //Class methods here

function my_widgets_init() {
    register_widget( 'Foo_Widget' );
add_action( 'widgets_init', 'my_widgets_init' );
  • Thanks...actually, I figured out the problem and finally got it working..thanks for answering!
    – Allen
    Commented Aug 21, 2012 at 23:35
  • Please post your solution, Allen. Commented Aug 27, 2012 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.