Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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() {

 register_widget('Foo_Widget');
 }

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.

share|improve this question
add comment

1 Answer

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' );
share|improve this answer
    
Thanks...actually, I figured out the problem and finally got it working..thanks for answering! –  Allen Aug 21 '12 at 23:35
    
Please post your solution, Allen. –  Zachary Schuessler Aug 27 '12 at 21:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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