$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - How to distinguish if a plugin is not installed or just not active|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin12PV0评论

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

Post a comment

comment list (0)

  1. No comments so far