我遇到的最好的方法是基于Ian Dunn 插件。我为Wordpress编写了一个插件,它依赖于Woocommerce,随后也需要Woocommerce的特定版本。为了实现这一点,我编写了以下代码。重要的是要注意,这里回答您问题的重要性是require_once( ABSPATH . \'/wp-admin/includes/plugin.php\' ) ;
尽早调用此文件后,可以验证哪些插件处于活动状态以及这些插件的版本。下面是一个简短的演示:
define ( \'WCCF_NAME\', \'Woocommerce Plugin Example\' ) ;
define ( \'WCCF_REQUIRED_PHP_VERSION\', \'5.4\' ) ; // because of get_called_class()
define ( \'WCCF_REQUIRED_WP_VERSION\', \'4.6\' ) ; // because of esc_textarea()
define ( \'WCCF_REQUIRED_WC_VERSION\', \'2.6\' ); // because of Shipping Class system
/**
* Checks if the system requirements are met
*
* @return bool True if system requirements are met, false if not
*/
function wccf_requirements_met () {
global $wp_version ;
require_once( ABSPATH . \'/wp-admin/includes/plugin.php\' ) ; // to get is_plugin_active() early
if ( version_compare ( PHP_VERSION, WCCF_REQUIRED_PHP_VERSION, \'<\' ) ) {
return false ;
}
if ( version_compare ( $wp_version, WCCF_REQUIRED_WP_VERSION, \'<\' ) ) {
return false ;
}
if ( ! is_plugin_active ( \'woocommerce/woocommerce.php\' ) ) {
return false ;
}
$woocommer_data = get_plugin_data(WP_PLUGIN_DIR .\'/woocommerce/woocommerce.php\', false, false);
if (version_compare ($woocommer_data[\'Version\'] , WCCF_REQUIRED_WC_VERSION, \'<\')){
return false;
}
return true ;
}
function wccf_requirements_error () {
global $wp_version ;
require_once( plugin_dir_path ( __FILE__ ) . \'/admin/partials/requirements-error.php\' ) ;
}
if ( wccf_requirements_met() ) {
require_once( __DIR__ . \'/classes/wpps-module.php\' );
require_once( __DIR__ . \'/classes/wordpress-plugin-skeleton.php\' );
require_once( __DIR__ . \'/includes/admin-notice-helper/admin-notice-helper.php\' );
require_once( __DIR__ . \'/classes/wpps-custom-post-type.php\' );
require_once( __DIR__ . \'/classes/wpps-cpt-example.php\' );
require_once( __DIR__ . \'/classes/wpps-settings.php\' );
require_once( __DIR__ . \'/classes/wpps-cron.php\' );
require_once( __DIR__ . \'/classes/wpps-instance-class.php\' );
if ( class_exists( \'WordPress_Plugin_Skeleton\' ) ) {
$GLOBALS[\'wccf\'] = WordPress_Plugin_Skeleton::get_instance();
register_activation_hook( __FILE__, array( $GLOBALS[\'wccf\'], \'activate\' ) );
register_deactivation_hook( __FILE__, array( $GLOBALS[\'wccf\'], \'deactivate\' ) );
}
} else {
add_action( \'admin_notices\', \'wccf_requirements_error\' );
}