@david: Adam Backtrom和Viper007Bond都给出了一些很好的建议,所以我想我应该接受他们的建议,看看我是否能实现一些东西,见下文。
下面是一个名为WP Active Plugins Data 它会在任何插件被激活时解析所有活动插件的头元数据,并将每个插件的所有元数据存储到wp_options
. 我为常规WordPress插件和多站点范围的插件设计了它。你可以download it here from gist 但我也复制了这里的代码供您审阅:
<?php
/*
Plugin Name: WP Active Plugins Data
Plugin URI: http://mikeschinkel.com/wordpress-plugins/wp-active-plugins-data/
Description: Loads Plugin Data on Plugin Activation and Persists to wp_options for quick retrieval.
Version: 0.1
Author: Mike Schinkel
Author URI: http://mikeschinkel.com
Note: Written for http://wordpress.stackexchange.com/questions/361/is-there-a-way-for-a-plug-in-to-get-its-own-version-number
*/
require_once(ABSPATH.\'wp-admin/includes/plugin.php\');
function get_active_plugin_version($plugin_path_file, $sitewide = false) {
return get_active_plugin_attribute($plugin_path_file,\'Version\');
}
function get_active_plugin_attribute($plugin_path_file, $attribute) {
$all_plugins_data = get_active_plugins_data($plugin_path_file,$sitewide);
return (isset($all_plugins_data[$attribute]) ? $all_plugins_data[$attribute] : false);
}
function get_active_plugins_data($plugin_path_file, $sitewide = false) {
$failsafe = false;
$plugin = plugin_basename(trim($plugin_path_file));
$sitewide = (is_multisite() && ( $sitewide || is_network_only_plugin($plugin)));
if ($sitewide) {
$all_plugins_data = get_site_option(\'active_sitewide_plugin_data\',array());
} else {
$all_plugins_data = get_option(\'active_plugin_data\',array());
}
if (!$failsafe && !is_array($all_plugins_data) || count($all_plugins_data)==0) {
$failsafe = true; // Don\'t risk infinite recursion
if ($sitewide) {
$active_plugins = get_site_option(\'active_sitewide_plugins\',array());
} else {
$active_plugins = get_option(\'active_plugins\',array());
}
persist_active_plugin_data(null,$active_plugins,$sitewide);
$all_plugins_data = get_active_plugin_version($plugin_path_file,$sitewide);
}
return $all_plugins_data[$plugin_path_file];
}
add_action(\'update_site_option_active_sitewide_plugins\',\'persist_sitewide_active_plugin_data\',10,2);
function persist_sitewide_active_plugin_data($option, $plugins) {
persist_active_plugin_data(null,$plugins,\'sitewide\');
}
add_filter(\'update_option_active_plugins\',\'persist_active_plugin_data\',10,2);
function persist_active_plugin_data($old_plugins, $new_plugins, $sitewide=false) {
$active_plugin_data = array_flip($new_plugins);
$plugin_dir = WP_PLUGIN_DIR;
foreach($new_plugins as $plugin) {
$active_plugin_data[$plugin] = get_plugin_data("$plugin_dir/$plugin");
}
if ($sitewide)
update_site_option(\'active_sitewide_plugin_data\',$active_plugin_data);
else
update_site_option(\'active_plugin_data\',$active_plugin_data);
}
想看看它是怎么工作的吗?这里有一个测试文件,你可以放在WordPress网站的根目录下(
http://example.com/test.php
.) 在测试之前,请确保已激活此插件和Akismet。
<?php
/*
* test.php - Place in root of WordPress website.
*
* Before running be sure to activate both Akismet and the WP Active Plugin Data plugin
*
*/
include "wp-load.php";
header(\'Content-type:text/plain\');
$akismet = "akismet/akismet.php";
echo "Akismet Version: " . get_active_plugin_version($akismet);
echo "\\n\\nAkismet Description: " . get_active_plugin_attribute($akismet,\'Description\');
echo "\\n\\nAll Akismet Data:\\n";
print_r(get_active_plugins_data($akismet));
如果这不完全是你需要的,至少它应该给你一个良好的起点。希望这有帮助。