在另一个 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');
  • 我认为您在辅助文件中的路径不正确。
  • 试试这个包括('anotherfolder/secondaryfile.php');

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

$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";

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

当您在 WordPress 插件中使用 PHP includerequire 时,最好使用 WordPress 特定函数来获取文件的绝对路径。您可以使用 plugin_dir_path

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

插件目录并不总是在 wp-content/plugins,因为我们可以在 WordPress Codex:

中阅读

It's important to remember that WordPress allows users to place their wp-content directory anywhere they want, so you must never assume that plugins will be in wp-content/plugins, or that uploads will be in wp-content/uploads, or that themes will be in wp-content/themes.

因此,使用 plugin_dir_path() 可确保 include 始终指向正确的路径。