Wordpress - no such file or directory issue

2019-03-04 19:40发布

问题:

I am getting this error when I load up my Wordpress site:

Warning: include_once(../../plugins/acf-location-field-master/acf-location.php) [function.include-once]: failed to open stream: No such file or directory in C:\wamp\www\cmpmushrooms.tld\wp-content\themes\shrooms_v0.1\functions.php on line 455

The code it is referring to is this:

function my_register_fields()
{
    include_once('../../plugins/acf-location-field-master/acf-location.php');
}

I realise it is saying it cannot find the file but why is that? because it exists in the directory.

回答1:

You should do this way to include file from WP plugins directory :

include( plugin_dir_path( __FILE__ ) . 'plugin-name/file-name.php');

If you want to include all files within that directory, use it like this:

foreach ( glob( plugin_dir_path( __FILE__ )."plugin-name/*.php" ) as $file )
    include_once $file;

Have fun :)



回答2:

Why not:

include_once ( WP_PLUGIN_DIR . '/acf-location-field-master/acf-location.php');


标签: php wordpress