0

Where do we add our custom widget? I keep on getting internal errors, I tried to add the class in wp_content > plugins, or wp_includes > widgets, but I get an internal error.

The class is simple, inside "My_Widget.php" :

<?php
/*
  Plugin Name: My_Widget
  Description: my widet description
  Author: Author
 */
class My_Widget extends WP_Widget {

    /**
     * Sets up the widgets name etc
     */
    public function __construct() {
        $widget_ops = array( 
            'class_name' => 'my_widget',
            'description' => 'My Widget is awesome',
        );
        parent::__construct( 'my_widget', 'My Widget', $widget_ops );
    }

    /**
     * Outputs the content of the widget
     *
     * @param array $args
     * @param array $instance
     */
    public function widget( $args, $instance ) {
        // outputs the content of the widget
    }

    /**
     * Outputs the options form on admin
     *
     * @param array $instance The widget options
     */
    public function form( $instance ) {
        // outputs the options form on admin
    }

    /**
     * Processing widget options on save
     *
     * @param array $new_instance The new options
     * @param array $old_instance The previous options
     */
    public function update( $new_instance, $old_instance ) {
        // processes widget options to be saved
    }
}

Then in my functions.php :

register_widget( 'My_Widget' );
2
  • maybe the errors are not related to you class, but to some problem with your file like wrong encoding, or some misconfiguration of the server. You should specify what errors you actually get and if they are generic 500, then check you php error log and see with there is anything more detailed there Jan 31, 2016 at 5:43
  • What error are you getting? Please file an edit with your debugging info. Also, you should register your widget in your plugin. Jan 31, 2016 at 5:48

2 Answers 2

1

I copied the class definition and register_widget( 'My_Widget' ); to my theme's functions.php file & it did show My Widget under Appearance > Widgets. So please place this code directly in your theme's functions.php file. It is not recommended to edit anything in wp-includes directory.

1
  • Ok thank you, I added the file inside the theme folder and I used require get_template_directory() . "My_Widget.php" and it worked.
    – Paul
    Jan 31, 2016 at 19:03
-1

You have 2way to active your class .

  1. you can create a folder inside plugins folder ...
  2. you can include in functions.php like as Bellow

    require_once(get_template_directory().'My_Widget.php'); 
    
1
  • There is no need to create folder inside plugins folder and then include it in functions.php file like you mentioned. Please be sure while you suggest an answer because that unnecessarily wastes the time of person in need. Jan 31, 2016 at 15:01

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