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.

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?

0

2 Answers 2

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 );
4
  • 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? :/
    – rkoller
    Nov 29, 2018 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? Nov 29, 2018 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.
    – rkoller
    Nov 29, 2018 at 11:22
  • $pathpluginurl = WP_PLUGIN_DIR . '/advanced-custom-fields/acf.php'; if ( file_exists( $pathpluginurl )) { ... also works too. Dec 12, 2022 at 17:01
0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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