2

I am learning Wordpress development with a tutorial where we learn to create a widget, it is asked to create a file named newsletterwidget.php with the following code, but it does not say in which folder to put it:

class Zero_Newsletter_Widget extends WP_Widget
{
    public function __construct()
    {
        parent::__construct('zero_newsletter', 'Newsletter', array('description' => 'Un formulaire d\'inscription à la newsletter.'));
    }
    public function widget($args, $instance)
    {
        echo 'widget newsletter';
    }
}

So, as WP_Widget class is created in wp-includes\class-wp-widget.php, I decided to put newsletterwidget.php in wp-includes but I get the following error:

Fatal error: Class 'WP_Widget' not found in C:\xampp\htdocs\wordpress\wp-includes\newsletterwidget.php on line 5

Thank you

1 Answer 1

1

You should never work on core files which are inside wp-admin and wp-includes folder.

You'll be just working on wp-content folder.

If you're developing a plugin, you can directly put the code in your main plugin file or you can put the file inside of your plugin folder and include that file in the plugins main file.

Or if you're working with a theme, you can put the code directly in functions.php or create the php file inside your theme folder and include that file in functions.php

3
  • Ok thank you, I added include 'C:\xampp\htdocs\wordpress\wp-includes\class-wp-widget.php'; to my newsletterwidget.php file, but now when I activate the plugin I get an error: Fatal error: Cannot redeclare class WP_Widget in C:\xampp\htdocs\wordpress\wp-includes\class-wp-widget.php on line 20
    – ThisIsMe
    Commented Apr 8, 2017 at 12:00
  • sorry, I just replace include with include_once, no more error
    – ThisIsMe
    Commented Apr 8, 2017 at 12:03
  • Great to know that you got it solved. Commented Apr 8, 2017 at 12:11

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.