With the custom post type enhancements introduced in WordPress 3, site builders and plugins developers now have access to what you could call “dynamic scaffolding”. With a couple of lines of code, you can easily generate the necessary UIs to help users easily manage and create new types of content.
You can add new post types (e.g. Movies, Reviews, Products, Projects, etc.) and specify the features each should support (e.g. title, body, authors, revisions, etc.) . Beyond that, you can add use the ever-flexible hook and filter system to add your own features. Combine that with custom fields and you can add your own fields (so a “movie” post type can have new fields to add things like “release date”, “rating”, and so on) .
However the code involved in adding those additional fields can get cumbersome, especially when you start looking at lots of content types with numerous fields. Add in other object types like users and comments and you’re now looking at a huge code base that needs to be maintained, updated and so on.
Well, I’m making it easier.
Custom Metadata Manager introduces a very WordPress-like way of adding additional fields to your object types. The goal is to help you rapidly build familiar, intuitive interfaces for your users.
That one line of code kicks into gear some of that “dynamic scaffolding” magic I mentioned earlier. The Edit Post page now has a metabox with a text field. The plugin handles all the heavy-lifting for you behind-the-scenes, so that you can focus on more on building out and connecting your data rather than all the minor details. Checkit:
The API is similar to that used for registering custom post types and taxonomies so it should be familiar territory.
But let’s not stop there. Let’s say I want comments and users to all have this magical new field. Let’s try something like this:
And we get the same field added for users and comments!
Note: I’ve omitted a screenshot for comments since it looks exactly the same as the posts screen.
Custom Metadata Manager supports fields for:
- posts (built-in and any custom post types)
If/when links and taxonomies get metadata support, I’ll add support for them as well.
Labels and Descriptions!
Avoid cryptic slug-like names and make it easy for users by assigning friendly labels and descriptions!
'Field Label', 'description' => 'This is a friendly description for the field to help you enter your data.' ) ); ?>
To avoid clutter on the Edit Post page, you can group sets of fields together.
'Group' ) ); x_add_metadata_field( 'field-1', 'post', array( 'group' => 'group-1' ) ); x_add_metadata_field( 'field-2', 'post', array( 'group' => 'group-1' ) ); ?>
Text fields can get boring, so I’ve cooked in a number of different field types (just specify the field_type in the $args parameter and go!):
Notice that last one in the screenshot above? That’s using a custom display callback! You can override a bunch of different things (display callback, sanitize callback, etc.) to get even more control over the fields you add. That means you can do cool stuff like this:
We’ll throw in an easy way to add your fields to the Manage Post/User/Comments pages as well (in your $args, just set display_column to true). By default, the column will just the value of the field, but if you’ve got something complex going on, you can just as easily include a display_column_callback.
Why this plugin?
Don’t get me wrong: this isn’t anything revolutionary. Lots and lots and lots and lots have come before me. But my approach is probably the most “WordPress-like” there is (Bonus points to Easy Custom Fields which uses a code-based and very object-oriented approach).
The API is simple, intuitive and very flexible, providing lots of overrides if you don’t like the default functionality (or have specific use cases).
The plugin doesn’t use extra tables and stores all data in a WordPress native way, so you can use the standard get_metadata function to retrieve your custom data. That way you also don’t have to worry about losing your data should you choose to stop using this plugin.
(I have already deployed and used the plugin across a number of production sites and it’s working great and saving me significant amounts of time and code!)
Why a code-based approach instead of a UI?
Because most of the plugins I mentioned above do the UI thing (and some it really well!) and it’d be lame to just copy them. The code-based approach, though, more closely aligns with the existing WordPress approach of registering new types of content (post types, taxonomies, etc.).
This is also a developer feature, aimed towards site builders. And real developers don’t need UIs ;)
What stuff am I cooking up for future versions?
- Improved styling of rendered fields
- Ability Pass in attributes for built-in fields (e.g. class, data-*, etc.)
- Additional field types
- Limit or exclude groups and fields for specific ids (e.g. show only on post id = 145)
- Autosave support for fields on post types
- Option to enqueue scripts and styles
- Client- and server-side validation support
- Add groups and fields to Quick Edit
Grab it from the WordPress Plugin Directory or just search for “Custom Metadata Manager” from the Plugin Manager within your WordPress install.
If you’ve read all the way through, thanks! Like what you see? Want more field types and features added? Just want say hi? Get in touch.