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