php - How to distinguish if a plugin is not installed or just not active

admin2025-06-06  2

I try to apply a check figuring out if a plugin is either not active or just not installed at all in the plugin directory. When I test I check for a plugin installed but not activated as seen in the screenshot.

The checking if inactive part works flawlessly:

$isinactive = is_plugin_inactive( 'advanced-custom-fields/acf.php' );
var_dump( $isinactive );

While checking if the plugin is actually installed and present in the plugin directory does not. First I generate the path to the main plugin file:

$pathpluginurl = plugins_url( 'advanced-custom-fields/acf.php' );
var_dump($pathpluginurl);

Then check if that file exists:

$isinstalled = file_exists( $pathpluginurl );
var_dump($isinstalled);

And then check if the particular file does NOT exist:

if ( ! file_exists( $pathpluginurl ) ) {
    echo "File does not exist";
} else {
    echo "File exists";
}

The output:

true //true for that the plugin is not active
/wp-content/plugins/advanced-custom-fields/acf.php
false  // false for that the acf.php file actually exists

file not exists

I don't understand why file_exists is not stating the facts and states the contrary saying the plugin does not exist?

I try to apply a check figuring out if a plugin is either not active or just not installed at all in the plugin directory. When I test I check for a plugin installed but not activated as seen in the screenshot.

The checking if inactive part works flawlessly:

$isinactive = is_plugin_inactive( 'advanced-custom-fields/acf.php' );
var_dump( $isinactive );

While checking if the plugin is actually installed and present in the plugin directory does not. First I generate the path to the main plugin file:

$pathpluginurl = plugins_url( 'advanced-custom-fields/acf.php' );
var_dump($pathpluginurl);

Then check if that file exists:

$isinstalled = file_exists( $pathpluginurl );
var_dump($isinstalled);

And then check if the particular file does NOT exist:

if ( ! file_exists( $pathpluginurl ) ) {
    echo "File does not exist";
} else {
    echo "File exists";
}

The output:

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

I don't understand why file_exists is not stating the facts and states the contrary saying the plugin does not exist?

Share Improve this question asked Nov 29, 2018 at 9:53 rkollerrkoller 6343 gold badges14 silver badges26 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 5

file_exists expects a path, not a URL. To get the path to an arbitrary plugin you'll need to use WP_PLUGIN_DIR:

$pathpluginurl = WP_PLUGIN_DIR . '/advanced-custom-fields/acf.php';

$isinstalled = file_exists( $pathpluginurl );

you are using file_exists() function on an URL not on path to file, this will not work (in most cases).

Instead of

$pathpluginurl = plugins_url( 'advanced-custom-fields/acf.php' );

you should do to get an absolute file path

$pathpluginurl = ABSPATH . 'wp-content/plugins/advanced-custom-fields/acf.php';

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749142875a316730.html

最新回复(0)