issues including a file from a plugin directory

I have a processForm.php in theme directory that does what it says. I am trying to include a file (which i need in order to run a specific function in processform.php) from a plugin directory and have been unable to do so. As suggested in first solution in this thread I tried :

include( plugin_dir_path( __FILE__ ) . 'test-plugin/needed_file.php'); 

I was pretty sure this would work but unfortunately it throw a warning like so :

Warning: include(/home2/xxx/public_html/wp-content/themes/xxx/test-plugin/needed_file.php) [function.include]: failed to open stream: No such file or directory 

As said earlier the processForm.php lies in the theme directory and I have no issues anywhere else, calling files within the template directory. If this may help, there is alos a constant defined for this particular plugin’s path which goes like this :

define('WPFP_PATH', plugins_url() . '/test-plugin'); 

So in accordance to a solution suggested here, I tried using code below :

include(WPFP_PATH . '/needed_file.php');

Unfortunately it throws three types of warning :

First warning :

http:// wrapper is disabled in the server configuration by allow_url_include=0 

Second warning :

failed to open stream: no suitable wrapper could be found in....

Third warning :

Failed opening 'http://awesomeness.com/wp-content/plugins/test-plugin/needed_file.php' for inclusion (include_path=".:/usr/lib/php:/usr/local/lib/php") in.....

So the bottomline is How do I include this file into my
processForm.php ( which is in root of theme directory).

1 Answer
1

The function plugin_dir_path has an misleading name, it doesn’t include a file from plugin directory, it just include a file from the same directory of the file passed as argument.

When you call

include( plugin_dir_path( __FILE__ ) . 'test-plugin/needed_file.php');

from a file in theme directory, you are just trying to include a file from theme directory too, because __FILE__ constant always contain the path of file in which the statement is wrote.

The second way you try is the right one, but when you define WPFP_PATH you should use the path of the file, not the url, because a lot of systems for security reasons have the including of urls disabled.

So you first have to put in main plugin file (the one that contain plugin headers)

define( 'WPFP_PATH', plugin_dir_path( __FILE__ ) ); 

and thene in the theme

include( WPFP_PATH . 'needed_file.php' );

will work.

Note that the file is wrote without leading slash, because plugin_dir_path return the path with trailing slash.

However, once WPFP_PATH is in the global namespace you should check if defined and/or maybe use a function to return the path, something like

function wpfp_get_path() {
  return plugin_dir_path( __FILE__ );
}

and then in theme include( wpfp_get_path() . 'needed_file.php' );

Leave a Comment