5

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
    Possible duplicate of Custom Widget Creating PHP Exception When Placed In Its Own PHP File - I just remembered that this appeared here before. BTW tried it on my local set-up right now, it does work flawlessly with the file name widgets.php, although the difference is that I'm just using some dummy code, so no real widget initialization. Additionally my local server might just be better configured, if thats the reason, see possible duplicate for more info. – Nicolai Aug 27 '15 at 13:16
  • I do think it's exactly the same issue, however the answer @Rarst provided is way better than the answer provided in the linked question. – Wouter Vandenneucker Aug 27 '15 at 13:53
  • I think so too, thats why I left a note at the other thread. – Nicolai Aug 27 '15 at 13:57
8

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' );

  • This indeed fixes the problem! Thank you very much! – Wouter Vandenneucker Aug 27 '15 at 13:52
  • But does this actually explain, why it depends on the file name? And if so, how? Or is it just, that then the second - or whatever - path looked at is the one of the file? – Nicolai Aug 27 '15 at 14:08
  • 1
    @ialocin that's just how PHP works, I've added quote from relevant part of manual. – Rarst Aug 27 '15 at 14:24
  • I should have figured - and I kind of did -, especially because I cited the same two sentences on the other question. But at least it is stated explicitly here too now. – Nicolai Aug 27 '15 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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