Extracting Plugin Metadata
Have you ever dreamed of extracting useful metadata from WordPress plugins? Probably you haven’t. But if you ever need a way to parse a plugin’s readme.txt, or want to simplify metadata generation for the custom update checker, here’s a couple of utility functions that may come in handy. I originally wrote them for internal use, but I imagine they may be useful to some other plugin developers as well.
Demo & Download
- Live demo
Demonstrates how one can extract various bits of information from a plugin’s ZIP file. Note: Uploaded files are removed immediately after they’ve been processed. - Source code
Includes the utility functions, the source code of the demo, and a complimentary copy of a Markdown parser (used to process readme.txt files).
Function Reference
To use any of these functions, you will first need to include
“plugin-meta/plugin-meta.php"
from the “source code” link above.
parsePluginReadme($readmeTxtContents, $applyMarkdown = false)
Parse a plugin’s readme.txt to extract various plugin metadata. The first argument should be the contents of a valid readme.txt file. By default, any Markdown markup will not be processed and will instead be returned as-is. If you want the function to convert it to HTML, specify True as the second argument.
Returns an associative array with the following fields :
name
– Name of the plugin.contributors
– An array of wordpress.org usernames.donate
– The plugin’s donation link.tags
– An array of the plugin’s tags.requires
– The minimum version of WordPress that the plugin will run on.tested
– The latest version of WordPress that the plugin has been tested on.stable
– The SVN tag of the latest stable release, or ‘trunk’.short_description
– The plugin’s “short description”.sections
– An associative array of sections present in the readme.txt. Case and formatting of section headers will be preserved.
If the input string doesn’t contain the expected readme.txt header, the function will return NULL instead.
Be warned that this function does not perfectly emulate the way that WordPress.org parses plugin readme’s. In particular, it may mangle certain HTML markup that wp.org handles correctly.
getPluginHeader( $file_data )
Parse the plugin contents to retrieve plugin’s metadata headers. This is a simplified version of the get_plugin_data() function used by WordPress. Unlike the original function, it takes only one argument – the contents of a plugin’s PHP file – and returns NULL if the input string doesn’t appear to contain a valid plugin header. Refer to the get_plugin_data documentation for more details.
getPluginPackageMeta( $packageInfo )
Extract plugin metadata from a plugin’s ZIP file and transform it into a structure compatible with the custom update checker. When calling this function, pass the full path to a ZIP file containing a WP plugin as the argument.
This is a utility function that scans the input file (assumed to be a ZIP archive) to find and parse the plugin’s main PHP file and readme.txt file. Plugin metadata from both files is assembled into an associative array. The structure if this array is compatible with the metadata file format used by the custom plugin update checker – visit that link for more details.
Related posts :
Would this be difficult to integrate with the custom update checker?
It would probably be relatively easy. I actually use this to generate the .json update info file for one of my plugins.