最近在研究WordPress多站点创建WordPress的Saas自助建站平台,过程中我需要在前台显示该平台中所有预制插件的信息,并以列表的形式显示出来,以方便用户查看。于是用到了函数get_plugins();
get_plugins()获取某个目录下的所有插件信息
描述
Check the plugins directory and retrieve all plugin files with plugin data.
WordPress only supports plugin files in the base plugins directory (wp-content/plugins) and in one directory below the plugins directory (wp-content/plugins/my-plugin). The file it looks for has the plugin data and must be found in those two locations. It is recommended that you keep your plugin files in directories.
The file with the plugin data is the file that will be included and therefore needs to have the main execution for the plugin. This does not mean everything must be contained in the file and it is recommended that the file be split for maintainability. Keep everything in one file for extreme optimization purposes.
用法
<?php get_plugins( $plugin_folder ) ?>
参数
$plugin_folder
(string) (可选) Relative path to single plugin folder.
默认值: ''
返回值
(array)
Key is the plugin file path and the value is an array of the plugin data.
事例
Get All Plugins
The following code snippet returns all plugins installed on your site (not just activated ones).
/* ----------------------------------
* wordpress之魂 © http://wphun.com
* ---------------------------------- */
<?php
// Check if get_plugins() function exists. This is required on the front end of the
// site, since it is in a file that is normally only loaded in the admin.
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$all_plugins = get_plugins();
// Save the data to the error log so you can see what the array format is like.
error_log( print_r( $all_plugins, true ) );
Example output:
/* ----------------------------------
* wordpress之魂 © http://wphun.com
* ---------------------------------- */
Array
(
[hello-dolly/hello.php] => Array
(
[Name] => Hello Dolly
[PluginURI] => http://wordpress.org/extend/plugins/hello-dolly/
[Version] => 1.6
[Description] => This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.
[Author] => Matt Mullenweg
[AuthorURI] => http://ma.tt/
[TextDomain] =>
[DomainPath] =>
[Network] =>
[Title] => Hello Dolly
[AuthorName] => Matt Mullenweg
)
源文件
get_plugins() 函数的代码位于 /wp-admin/includes/plugin.php
.
/* ----------------------------------
* wordpress之魂 © http://wphun.com
* ---------------------------------- */
/**
* Check the plugins directory and retrieve all plugin files with plugin data.
*
* WordPress only supports plugin files in the base plugins directory
* (wp-content/plugins) and in one directory above the plugins directory
* (wp-content/plugins/my-plugin). The file it looks for has the plugin data
* and must be found in those two locations. It is recommended to keep your
* plugin files in their own directories.
*
* The file with the plugin data is the file that will be included and therefore
* needs to have the main execution for the plugin. This does not mean
* everything must be contained in the file and it is recommended that the file
* be split for maintainability. Keep everything in one file for extreme
* optimization purposes.
*
* @since 1.5.0
*
* @param string $plugin_folder Optional. Relative path to single plugin folder.
* @return array Key is the plugin file path and the value is an array of the plugin data.
*/
function get_plugins($plugin_folder = '') {
if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
$cache_plugins = array();
if ( isset($cache_plugins[ $plugin_folder ]) )
return $cache_plugins[ $plugin_folder ];
$wp_plugins = array ();
$plugin_root = WP_PLUGIN_DIR;
if ( !empty($plugin_folder) )
$plugin_root .= $plugin_folder;
// Files in wp-content/plugins directory
$plugins_dir = @ opendir( $plugin_root);
$plugin_files = array();
if ( $plugins_dir ) {
while (($file = readdir( $plugins_dir ) ) !== false ) {
if ( substr($file, 0, 1) == '.' )
continue;
if ( is_dir( $plugin_root.'/'.$file ) ) {
$plugins_subdir = @ opendir( $plugin_root.'/'.$file );
if ( $plugins_subdir ) {
while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
if ( substr($subfile, 0, 1) == '.' )
continue;
if ( substr($subfile, -4) == '.php' )
$plugin_files[] = "$file/$subfile";
}
closedir( $plugins_subdir );
}
} else {
if ( substr($file, -4) == '.php' )
$plugin_files[] = $file;
}
}
closedir( $plugins_dir );
}
if ( empty($plugin_files) )
return $wp_plugins;
foreach ( $plugin_files as $plugin_file ) {
if ( !is_readable( "$plugin_root/$plugin_file" ) )
continue;
$plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
if ( empty ( $plugin_data['Name'] ) )
continue;
$wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
}
uasort( $wp_plugins, '_sort_uname_callback' );
$cache_plugins[ $plugin_folder ] = $wp_plugins;
wp_cache_set('plugins', $cache_plugins, 'plugins');
return $wp_plugins;
}
WordPress日记主要承接WordPress主题定制开发、PSD转WordPress、WordPress仿站以及以WordPress为管理后端的小程序、APP,我们一直秉持“做一个项目,交一个朋友”的理念,希望您是我们下一个朋友。如果您有WordPress主题开发需求,可随时联系QQ:919985494
相关文章
WordPress短代码教程:如何创建短代码,以及为什么它们非常有用
本文是一个深入的WordPress短代码教程。我们将介绍什么…WordPress自定义查询文章类WP_Query的详细介绍和使用方法
在WordPress主题开发中获取文章列表内容是最基本的功能…WordPress中如何获取指定用户角色的文章
今天,有用户在我们的WordPress中文社区问答区提问说,…WordPress获取文章列表函数get_posts()
get_posts()是WordPress用来获取最新或指定…WordPress删除指定文章自定义字段函数delete_post_meta()
在WordPress主题或WordPress插件开发的过程中…WordPress添加自定义字段到文章函数add_post_meta()
在我们开发WordPress主题或WordPress插件的时…