WordPress 4.6 to Standardize Metadata Registration

photo credit: Caio Resende

WordPress 4.6 will introduce some exciting updates that give plugin and theme authors a standardized way of registering metadata. This refers to the additional data that is attached to WordPress’ main content types (posts, users, and comments), which users often interact with via custom fields or metaboxes when editing posts. Anytime WordPress can standardize an approach for developers while making it more flexible at the same time, the ecosystem becomes more harmonious.

As of 4.6, the register_meta() function will allow for the registration of meta keys. According to the ticket for the enhancements, the goal was to “expand the scope of register_meta() so that specific information about that meta data could be registered in code rather than in the database, similar to register_content_type().”

When WordPress introduced the ability to register post types six years ago, it completely revolutionized plugins by giving developers a standard way to create new content types. The changes coming in 4.6 have the potential to do this for metadata. Although the ticket associated with these enhancements was originally created to help move the WP REST API project forward, the ability to register meta has many varied applications for plugin developers.

“It’s important for the upcoming WordPress REST API to be able to determine what pieces of data attached to a post can be exposed publicly,” WordPress core developer Jeremy Felt said. “The changes to register_meta() provide a framework for plugin and theme authors to register additional data so that more reliable assumptions can be made.”

Felt offered an example of a beer recipe plugin that captures specific information like mash time or grain profile in addition to a title and general description for the beer. All of these additional pieces of content are stored as metadata.

“In 4.6, it becomes easier for a project like the REST API to automatically include all of this data in a response rather than the usual basics,” Felt said. “I think being able to register meta will also be useful for projects other than the REST API. Similar to how registration of post types or taxonomies work now, there’s always much more you can do reliably when something has an explicit set of rules associated with it.”

Felt summarized the changes for developers on the Make/Core blog today:

A global variable, $wp_meta_keys, contains all registered meta keys.

The function signature of register_meta() has changed to support 3 arguments, the last being an array.

In the post, Felt details a list of the key/values which the array should use to contain data about the meta. WordPress 4.6 will also introduce a new register_meta_args filter to add support for additional arguments and a set of helper functions to make the global data more accessible.

Felt anticipates that there will be benefits for many other projects that build on top of meta, including current and future core features, as well as third-party extensions:

This data can one day be useful to everything from the Customizer, the editing experience for all object types, the Fields API, and many plugins that rely on metadata. Work will continue on transforming this $wp_meta_keys object and the methods surrounding it to be flexible and explicit.

As WordPress core becomes familiar with register_meta() and more confident in the approach, the set of default arguments can and likely will expand to include more data about registered meta keys as required.

For a more detailed account of the register_meta() enhancements, including code examples for registering meta in 4.6, check out Felt’s post on the Make/Core blog.