我试图应用一种检查,看看插件是否不是活动的,或者只是根本没有安装在插件目录中。当我测试时,我检查是否安装了插件,但没有激活,如屏幕截图所示。

检查是否不活动的部分工作得完美无缺:
$isinactive = is_plugin_inactive( 'advanced-custom-fields/acf.php' );
var_dump( $isinactive );在检查插件是否实际安装并显示在插件目录中时,没有。首先,我生成主插件文件的路径:
$pathpluginurl = plugins_url( 'advanced-custom-fields/acf.php' );
var_dump($pathpluginurl);然后检查该文件是否存在:
$isinstalled = file_exists( $pathpluginurl );
var_dump($isinstalled);然后检查特定文件是否不存在:
if ( ! file_exists( $pathpluginurl ) ) {
echo "File does not exist";
} else {
echo "File exists";
}产出:
true //true for that the plugin is not active
http://mysandbox.test/wp-content/plugins/advanced-custom-fields/acf.php
false // false for that the acf.php file actually exists
file not exists我不明白为什么file_exists没有陈述事实,相反地说插件不存在?
发布于 2018-11-29 10:01:43
file_exists需要路径,而不是URL。要获得通往任意插件的路径,您需要使用WP_PLUGIN_DIR:
$pathpluginurl = WP_PLUGIN_DIR . '/advanced-custom-fields/acf.php';
$isinstalled = file_exists( $pathpluginurl );发布于 2018-11-29 10:04:10
您在URL上使用file_exists()函数,而不是在文件路径上,这将不起作用(在大多数情况下)。
而不是
$pathpluginurl = plugins_url( 'advanced-custom-fields/acf.php' );
您应该做的是获得一个绝对的文件路径。
$pathpluginurl = ABSPATH . 'wp-content/plugins/advanced-custom-fields/acf.php';
https://wordpress.stackexchange.com/questions/320538
复制相似问题