Use Gravity Forms to submit custom post types
In my previous post I explained how I used the Types plugin to create a new custom post type. That custom post type will be used to display a table of supported themes for my WordPress SEO plugin, and is therefor called wpseo-theme. Now the trick here is that I want users to be able to submit themes through a form.
Gravity Forms + Custom Post Type addon
By default, Gravity Forms allows you to create posts through a form. It doesn’t have support for custom post types at the moment though, in part because a wonderful plugin was already created that allows for this. This plugin, aptly named Gravity Forms + Custom Post Types can be downloaded from WordPress.org.
Once you have both Gravity Forms and this plugin activated, you can start creating a form. The first step is to make the form fill our custom post type. We start with creating a form and dragging in a title field:
The title field can be found in the posts field section of Gravity Forms field, below the advanced fields:
Once you’ve added this input field and given it a name, go to the advanced section of its edit block, you’ll see an option to save as post type, this has been added by the afore mentioned plugin:
You check the box and select the custom post type you want to use, in my case, WPSEO Themes. Now we start adding the form. We need a couple of different types of values:
- The title: done.
- The “description”, which will just be the body text, so you can easily drag in the Body input field.
- An image, which should be saved as the featured image too, more on that below.
- Several custom fields, more below too.
Adding a featured image trough the form
This is actually pretty easy: drag in an image field and click edit, you’ll see something like the screen below:
As you can see, setting the image as featured image is as easy as ticking the box. It’s wise to also ask for a description if you don’t know what’s going to be on the image. In my case, it’s a screenshot of the theme, so I won’t bother and just set the alt tag automatically.
Adding custom fields through Gravity Forms
The next step is to add the several custom fields we need. In my case I had 5, but you can have as much as you want. You start by dragging a Custom Field input into your form. Once you have that, you click edit and you select the appropriate custom field type:
In this case, I’m asking for the theme URL, so I select website, but there are all sorts of options you can choose from, as you can see. Now here comes the tricky part, you need to set the name of your custom field. You should go into your Types -> Custom Fields page and check the second value below the custom field title:
That’s the name of your custom field, but you should prefix it with “wpcf-“, because that’s the Types plugin naming convention, which prevents its custom fields from clashing with other ones.
Of course, if you created a custom field group from already existing custom fields you don’t need to prefix the custom field name.
True / false or “boolean” input fields
Some of your custom values might be checkboxes, they’re either on or off, true or false. That’s called a boolean value in math / developers language, but for you, it’s really simple. Just create a custom field type “checkboxes”, and go into it’s settings:
Be sure to check the “enable values” box and set the value to just “1”. That way, if checked, Gravity Forms will save it as value “1” and the Types plugin will “get it”.
Deciding on workflow
Now, once you’ve used the above info to finish your form, you need to decide on a workflow. On the post title field, the one whose advanced settings we used to save this input as a custom post type, we now go to the “normal” properties:
As you can see, you can set a default post author and a post status. Now in my case the author will be me in most cases, as nobody will be logged in. However, if you have enabled registration on your site, you can force people to be logged in before even being able to use this form, by going into your forms advanced settings and checking the “require user to be logged in” checkbox:
This allows for all sorts of workflows, find one that suits your site!
We still haven’t written a single line of code, yet we’ve already created a custom post type and created a form that allows people to submit custom post types to us.
So, one more thing to check of off the to-do list:
Creating a custom post type + custom fields. Creating a form through which people can submit themes that fills this post type.
- Creating a browsable interface for this post type.
In my next post, I’ll explain how to use the Views plugin to create “views” for this post type and unveil the finished product!