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.

VSCode screen showing sidebar with the installed plugin and the editor screen with the code from underneath in the question

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?

up vote 2 down vote accepted

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 );
  • ahhhhh that way it works! Thanks! But one question is there an alternative way aside using that constant? Because when you check codex.wordpress.org/Determining_Plugin_and_Content_Directories it is recommended not to use those constants directly in plugins and themes? :/ – rpk Nov 29 at 10:13
  • 1
    No, there isn't. Probably because generally you shouldn't be messing about in other plugins' directories. What are you ultimately trying to achieve here? – Jacob Peattie Nov 29 at 10:51
  • I don't wanted to mess with the other plugins' directory. ;) I just wanted to check its state. Meaning based on the state in case of not installed as well as installed but inactive i wanted to display different admin notices with generated links for installing advanced custom fields or just activating it. – rpk Nov 29 at 11:22

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';

  • The content directory can be changed. You shouldn't hard-code a specific path for it. – Jacob Peattie Nov 29 at 10:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.