The short version: I made a Form API for WordPress. Use it in your plugin. Make better forms. Give back and help us all make better forms.
If you’ve every written a WordPress plugin, chances are you’ve had to create a form, be it for a settings page, custom post/user meta, widget configuration, front-end user input, or a myriad of other possibilities. If you look at WordPress core or most plugins, you’ll see all of them using pretty much the same method for creating a form field, something very similar to:
sprintf( '<input type="text" value="%s" name="%s" />', $current_value, $field_name ');
There are a few special kinds of fields that have pre-existing functions, such as
wp_dropdown_categories(). But generally speaking, we’re writing HTML in our PHP (maybe dressed up as a template include).
I want something better than that. I want a robust API that lets me create forms (or individual form elements), modify them, validate, and process user input. I want themes to have control over the display of forms. I want other plugins to be able to modify and extend my forms.
I tried a variety of existing libraries. Zend_Form from the Zend Framework has a lot to offer, in terms of a robust, object-oriented API. But it’s not very pluggable, and loading the entire Zend Framework just for the Form API is overkill.
I used to spend a lot of time in Drupal, and its Form API is somewhat pleasurable to work with. Forms are amazingly extensible and themeable. But I’m not thrilled with the array-based syntax, and porting it to WordPress would be inappropriate for a multitude of reasons.
Toward a Solution
In the grand hacker tradition, I was dissatisfied with the existing options, so I made my own. I’ve dubbed it WP Forms, and you can download/clone/fork it from its home on GitHub.
What does it do? It’s an API for forms in WordPress. You can use it to create simple form elements. You can use it to create entire forms. You can validate your forms. You can process the submitted information. You can change how elements are rendered. You can wrap elements in themeable markup.
There’s a lot of documentation. It should answer a lot of your questions. And those that I didn’t anticipate, I’d be delighted to answer.
Request for Feedback
WP Forms is surely incomplete. I’m personally not thrilled with the API around adding optgroups to select elements, but I’m not yet sure how to improve it. The API for decorating elements is probably a little complicated for beginning developers, so I’m open to suggestions for a better way to handle that (or at least provide a facade for most common operations). Beyond that, I just don’t know how people would want to use the API. I know how I would use it, but I’m not “people”, and I’m definitely not you; I’m just me.
I can think of several features to add: conditional fields/content, ajax submissions, multipage forms, media uploads, more built-in field types/validators. What features would you want to see?
Request for Contributions
WP Forms will be much more powerful if we can get behind it as a community, extending it, improving it, using it, testing it, documenting it. Do you want an awesome form API in WordPress? I hope you’ll help me make one.
5 thoughts on “WordPress Form API”
This looks really nice, super job! I’ve worked a bit with the Drupal forms api and it’s really handy. I’ve often dreamed of a similar thing in WordPress! Code looks fab, will try it out and let you know how I get on.
Massive thanks for an amazing tool.
This looks really interesting. I thought about building something similar myself, actually.
Are you still actively developing this?
Yes, but slowly. Feel free to post issues, ideas, and (especially) pull requests on github.
Hi. I would really like to try to implement this in my current form development.
I am new to WP and plugin programming.
I have build a form programatically using a API and JSON. My question is how would I use your API to build a form?
I need a pointer or two.
Thanks in advance.
Comments are closed.