tags:

views:

68

answers:

2

I've got some functions in my file that require taxonomy.php to execute. I'm attempting to include the file like so...(this is called from functions.php, which is located in wp_content/themes/mytheme/

include($_SERVER['DOCUMENT_ROOT'] .'/wp-admin/includes/taxonomy.php');

However, depending on the particular server setup, its hit or miss (mostly hit) whether the include will be found. In the case its not found, I get this error...

Warning: include(/home/user/domainname.com/wp-admin/includes/taxonomy.php) [function.include]: failed to open stream: No such file or directory in /home/user/domainname.com/testing123/wp-content/themes/mytheme/functions.php on line 10

*UPDATE: Assuming I'm already doing about as much as I can by using DOCUMENT_ROOT, is there a means of avoiding the error that occurs when the file is not found? And I will be checking for function_exists in any functions that require taxonomy.php so as to suppress errors...*

+1  A: 

Well, the only way to bulletproof this is to actually configure it on each server you deploy it on. If you can't assume document_root is a reliable factor, I can't imagine any other variable that could give you a better result.

You could start searching for taxonomy.php but that is expensive, and horribly prone to failure or picking the wrong file (what happens if my neighbor has wordpress installed in /home/user/domainname2.com?

Pekka
Thanks Pekka, I do believe that the current approach of document_root is best as well. I just need a means of suppressing the "failed to open stream" error if the file is not found.
Scott B
Ahh ok. You would have to check whether the file exists, using `file_exists()`.
Pekka
Thanks Pekka, I appreciate the help. I'll use file_exists in conjunction with function_exists to control error supression.
Scott B
A: 

If your application depends on a particular file, you should be using require/require_once to ensure your application does not partially load despite missing the required file.

As for making sure that file always exists and is in a place you need it, you could move that to a configuration file and allow the user running the software to set it, along with auto-attempting to find the find in the event the user is clueless.

Nick Presta
Hi Nick, the app has 2 functions that require taxonomy.php and they are not critical to the operation. I'm just auto creating a couple of categories in the event they don't already exist. The user can manually create the directories otherwise and their existence is not required.So, I suppose I just need error supression if the include file is not found and I can't seem to figure out how to suppress the "failed to open stream" error.
Scott B
You can obviously do that with an `@` although I am ashamed of posting that here ;) Bad practice. Instead, you should probably use `file_exists()` first...
Franz
Thanks Nick, I appreciate the help.
Scott B