包含一个php文件在另一个php文件wordpress插件


Include a php file in another php file wordpress plugin

我试图在另一个文件中包含一个文件,但没有。没有错误。首先,我包含了我的主文件。

include('inc/onefolder/mymainfile.php');

然后我尝试在这个文件(mymainfile.php)中包含次要文件。

include('inc/onefolder/anotherfolder/secondaryfile.php');

当你在WordPress插件中使用PHP includerequire时,使用WordPress特定的函数来获取文件的绝对路径是一个好主意。您可以使用plugin_dir_path:

include( plugin_dir_path( __FILE__ ) . 'inc/onefolder/mymainfile.php');

Plugins目录并不总是在wp-content/Plugins中,因为我们可以在WordPress Codex中看到:

重要的是要记住,WordPress允许用户放置他们的任何他们想要的Wp-content目录,所以你永远不能假设插件将在wp-content/Plugins中,或者上传将在Wp-content/上传,否则主题将在Wp-content/themes中。

因此,使用plugin_dir_path()确保include总是指向正确的路径。

尝试包含文件的绝对路径。使用它:

$file = ABSPATH."wp-content/plugins/your-plugin/your-file.php";

在你的例子中:

//main thing is to use ABSPATH
$file = ABSPATH."inc/onefolder/anotherfolder/secondaryfile.php";    
require( $file ); // use include if you want.

希望对你有帮助。

编辑:

$file = ABSPATH."wp-content/plugins/your-plugin/inc/onefolder/anotherfolder/secondaryfile.php";

查看一下,请注意,您应该给出上面一行所写的确切路径。

  • 我认为你在次要文件中的路径不正确。
  • 试试include('anotherfolder/secondaryfile.php');