Add New Headers to WordPress Plugins or Themes

I’ve had a few patches committed to WordPress core in the last few versions, and have sadly neglected to do writeups about them so that others could take advantage of the new features. I hope to remedy that in the next few posts. First off, WordPress 2.9 has a new feature that allows you to add to the list of headers that are checked when you run either get_plugin_data() or get_theme_data().

For example: Let’s say you want to make a plugin that works with other plugins. For other plugins to be compatible with it, however, they need to provide some new piece of information, and you want to do this with a custom header. So we’re going declare a new plugin header. Let’s call it… oh…. “Demo Header”. Here’s the code; just drop this in a plugin and activate:

function plugin_header_demo( $extra_headers ) {
	$extra_headers[] = 'Demo Header';
	return $extra_headers;
}
add_filter( 'extra_plugin_headers', 'plugin_header_demo' );

Pretty simple, eh? What we’ve just done is added “Demo Header” to the list of headers that WordPress will check for when get_plugin_data() is run. Now, along with Name, Version, Author, and so forth, a value for Demo Header is returned as well.

Note that this is only for cases where you want any plugin checked for this header. This changes the get_plugin_data() function and takes effect every time that function is run, on any plugin.

Want to do it for themes? The code is almost identical:

function theme_header_demo( $extra_headers ) {
	$extra_headers[] = 'Demo Header';
	return $extra_headers;
}
add_filter( 'extra_theme_headers', 'theme_header_demo' );

Note that this does not allow you to alter the pre-existing headers in any way. You can’t, for example, remove the “Name” header.

Personally, I intend to use this for a third-party update check plugin, which will also be incorporated into Strider Core. For that use, I’ll add an “Update URL” header (or similar).

Next up: How to define your own custom get_XXX_data() function that works like the ones for plugins or themes.

This entry was posted in Codecraft and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.