关于php:检查插件是否已经存在,如果不存在,请使用插件中的文件?

Check if a plugin already exists, if not, use files within my plugin?

我正在创建一个需要高级自定义字段的插件,这里有有关如何添加它的文档,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 1. customize ACF path
function my_acf_settings_path( $path ) {

    // update path
    $path = plugin_dir_path( __FILE__ ) . 'lib/advanced-custom-fields/';

    // return
    return $path;

}
add_filter('acf/settings/path', 'my_acf_settings_path');


// 2. customize ACF dir
function my_acf_settings_dir( $dir ) {

    // update path
    $dir = plugin_dir_url( __FILE__ ) . 'lib/advanced-custom-fields/';

    // return
    return $dir;

}
add_filter('acf/settings/dir', 'my_acf_settings_dir');


// 3. Hide ACF field group menu item
add_filter('acf/settings/show_admin', '__return_false');


/**
* Set Advanced Custom Fields to Lite mode, so it does not appear
* in the WordPress Administration Menu
*/

define( 'ACF_LITE', true );


// 4. Include ACF
include_once( plugin_dir_path( __FILE__ ) . 'lib/advanced-custom-fields/acf.php' );

问题是我需要检查ACF插件是否已经存在,然后再将其包含在我的插件中-否则会发生冲突。 如何执行以下操作:

1)检查该插件是否已经在Wordpress中存在(有基本版和专业版)。

2)如果存在,则使用该版本,如果不存在,则使用我的插件中的版本(即使用上面的代码)。


我认为is_plugin_active()符合要求,因为您只真正关心它是否已安装并激活:

1
2
3
4
5
<?php
    if ( is_plugin_active('advanced-custom-fields') ) {
        // do something
    }
?>

如果您需要检查它是否已安装(无论是否激活),也可以检查它是否处于非活动状态。


您可以通过使用以下命令检查插件是否已安装

1
2
3
4
if( class_exists('acf'){
  //acf plugin already exist;
  //do something.
}