How to Create Custom Post Meta Boxes in MODX

Recently I read this article on how to create custom "meta boxes", or places to input custom data, on your posts in WordPress. It's a pretty long process, with lots of steps, custom PHP code, and more.

So, I figured, why not show how to do that in MODX? In fact, why not show how much less work it takes? Or how it takes exactly 0 lines of PHP code? And how it's already in the core of MODX, and much more seamlessly done? And do it in less than 1 minute?

Sure, why not? Sounds fun!


What is a Meta Box?

In the article, they talk about "Meta boxes", which they explain as such:

Its purpose is to allow the user to select or enter information in addition to the main post content.

Sounds handy. Does MODX have that? Why, yes, it does. In fact, it has it in the core.

What is a Template Variable?

A Template Variable - or TV - in MODX is simply a custom field for your Resource (in WordPress they call it a Page). You assign it to any Templates - which are "containers" for your pages - you want. Want a custom field of "tags" and have a Blog Template, a News Template, and a Contact Template? Want that "tags" field on all of them? Simple as cake in MODX. And that's no lie.

To start, all you do in MODX to create a TV is click on the New TV option here:

And then simply name it whatever you want. We'll name ours "MyTV":

You can add a description as well, and even adjust the "Input Options" (or how it renders in the backend, such as a textbox, textarea, dropdown, tag field, checkbox, etc) and "Output Options" (regular text, img tag, URL, etc) for it as well. Then assign it to whatever Templates you want, and viola! You have a custom field:

Oh, and by the way - you could move this TV out of the Template Variables tab and into any other tab, if you wanted, making it a "first-class citizen" in the content. There's a nifty UI in MODX, called "Form Customization", to do that in. And your custom field is reference-able in your page with the nice little tag: [[*MyTV]]. No PHP code necessary at all.

In Summary

So, yes, in less than one minute you can create a custom field in MODX. I mean, if you want to go the WordPress route, you can, but I think I'll take the easy road...

Late Update

Some have argued that WP's Post Meta is attributed to Post Types, which are more like Resource Types rather than Template-related Variables. My argument is that is moot - when is a presentation of a Resource and its content not linked? The nomenclature may differ, but MODX's architecture of custom fields is more flexible and powerful according to the way that content is presented.


Comments (2)

  1. Marko Heijnen:
    Oct 08, 2011 at 09:13 AM

    I think the comparison with WordPress Meta boxes is wrong. I think the way how MODX does it is more between WordPress custom fields and meta boxes. Since with WordPress Meta boxes you are more flexible in the presentation and value checking.

    I do like the way how you can select what can of template variable it should be in MODX. That is really powerful and such easy thing is missing in WordPress by default. I wouldn't recommend a user to use custom fields in WordPress.

    What I did read in your post and what is new for me is that you can move template variables to another tab. That is what I like in WordPress. Everything is in the same view. With MODX it is a separate tab and that sometimes confuse users.

    For the rest good article what gave me some research material about moving templates variables.

    Reply

  2. lossendae:
    Oct 08, 2011 at 10:05 AM

    Thanks for this article, creating a TV is really simple and imo more simple than in Wordpress natively.

    Note that powerusers use different solution similar to TV in WP, allowing a similar degree of flexibility and speed (no php involved). But that requires 3rd party plugin and i believe it's out of the scope of this article.

    That being said, i disagree with this end statement : "My argument is that is moot - when is a presentation of a Resource and its content not linked?"

    Let's take a simple case :
    Imagine an individual who want to manage his blog with MODx. Like WP he would want a thumbnail.
    He would have to create a thumbnail TV and then link it to the template in use.
    Like in WP, he would also like to test many template to suit until he find one that suit his taste.
    With TV attached to Template, each time he would have to manually associate the thumbnail to the template.

    That also mean more documentation if you're a designer who want to give modx a similar theme to WP's one.
    The same goes for all functionalities not forcefully attached to a single template (sharing bar, sidebar manager, etc...).
    This does not make MODx really template store ready (from this particular stand point) and i believe that it's a shame because MODx can definitively provide more leverage than it is now.

    What would be cool is to have both approach (TV attached to Template like it is now, and custom field attached to resource type) to allow more vertical integration.

    Note that WP themes allow metaboxes tied to the theme in use, therefore, it has some kind of TV + general metas while modx can only do it one way. That does not make WP better, but at least it can do it...

    Stephane.

    Reply


Add a Comment





Allowed tags: <b><i><br>Add a new comment:


Hi! Why don't you Login or Register?

Social

Latest Posts

Latest Comments

Tweets

GitHub Activity