In order to keep things clean I use functions.php almost solely to require other files. As such part of functions.php looks like this:

#include types
require("includes/types.php");
#include shortcodes
require("includes/shortcodes.php");
#include widgetareas
require("includes/widgetareas.php");
#include paging navs
require("includes/paging-navs.php");
#include widgets
require("includes/widgets.php");

themefolder/includes/widgets.php looks like this:

/*columns widget*/
require("widgets/columns-widget.php");

And a working widget is written in themefolder/includes/widgets/columns-widget.php

Now here is the strange part: this code doesn’t break or doesn’t throw any errors. Yet the code written in themefolder/includes/widgets.php doesn’t get processed and my columns-widget.php doesn’t load.

On the other hand, if I rename themefolder/includes/widgets.php to themefolder/includes/widget.php and change the corresponding line in functions.php to

#include widgets
require("includes/widget.php"); 

the code runs as expected and my columns-widget loads.

Why is this? Is this a bug or is it intended behavior?

1
1

The first location where PHP would look for relative path will not be the file you specify include in, but relative to point of entry file (index.php or whatever).

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn’t found in the include_path, include will finally check in the calling script’s own directory and the current working directory before failing.

http://php.net/manual/en/function.include.php

My guess would be that your code picks up wp-admin/includes/widgets.php (because in admin context the point of entry is likely wp-admin/index.php) instead of your file.

Always write includes like require( __DIR__ . '/includes/widgets.php' );

Leave a Reply

Your email address will not be published. Required fields are marked *