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' );
  • 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 – Mark Kaplun Jan 31 '16 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. – Pieter Goosen Jan 31 '16 at 5:48
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.

  • 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 '16 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'); 
    
  • 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. – Prasad Nevase Jan 31 '16 at 15:01

protected by Community Mar 8 '16 at 14:08

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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