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:

Create form with Gravity Forms

The title field can be found in the posts field section of Gravity Forms field, below the advanced fields:

Post fields in Gravity Forms

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:

title field advanced section - save as custom post type

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:

Image field - featured image

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:

Select 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:

Custom field details - Types plugin

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.

Name custom field

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:

Custom field type checkboxes

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:

Post title - field 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:

Require log-in

This allows for all sorts of workflows, find one that suits your site!

Conclusion

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:

  1. Creating a custom post type + custom fields.
  2. Creating a form through which people can submit themes that fills this post type.
  3. 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!

Tags: ,


Yoast.com runs on the Genesis Framework

Genesis theme frameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides you with the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Read our Genesis review or get Genesis now!

33 Responses

  1. Rev. VoodooBy Rev. Voodoo on 6 February, 2012

    Man, where were you 6 months ago!! lol. I was setting up a user oriented wine rating site. I did all of this by code originally. The multiple detailed front end posting forms were hell for a non-experienced coder hack like myself. I did finally switch over to Gravity Forms. But wow, the process you are walking us through is pretty much what I had to stumble through the hard way. Thanks for a valuable series!

    • BrandonBy Brandon on 6 February, 2012

      Rev. Voodoo,

      Was wondering if your Wine Rating site is “live”? Would love to check it out!

      Thanks – Brandon R.

      • SteffyBy Steffy on 10 February, 2012

        Me too I would be interrested since I have planned to do a rating site too.. I’m very interrested in your rating systeme choice
        thank you in advance

  2. SimonBy Simon on 6 February, 2012

    This plugin is a fantastic addition to Gravity Forms – I’ve combined it with the PayPal add-on for creating a pretty powerful event registration system.

    The form populates a (non-public) post type of “attendee” with various custom fields & taxonomies for address, email, ticket type, accommodation, etc., so the site administrator can for example easily see a list of all attendees who’ve bought a premium ticket.

    Works like a dream and took me less than 3 hours to build! The only coding was for the template and a CSV export function.

    • PaulBy Paul on 9 February, 2012

      SImon – would you care to share a link to that event registration site? I currently look after a site I built for a client which does exactly that, however, I’d love to be able to rewrite it using Gravity Forms…

      • SimonBy Simon on 10 February, 2012

        Sorry,I can’t give out the URL until it goes live because the event is not publicly announced yet, probably in 2-3wks. You wouldn’t actually see anything from the front-end anyway, just the registration form, all the magic happens behind the scenes :-)

  3. Rajesh NamaseBy Rajesh Namase on 6 February, 2012

    Gravity is very good paid plugin, with addition of this plugin it simply rocks. Amazing.

  4. Chuck ReynoldsBy Chuck Reynolds on 6 February, 2012

    I’ve used this a few times and it works well. Where I ran into issues is if my CPT doesn’t use the title field. I have custom metaboxes for everything but the CPT must have the default title field for this to work. Not the end of the world but that dependency has been an issue and probably will be in the future for customized sites.

  5. todd hamiltonBy todd hamilton on 8 February, 2012

    This write up was really helpful, thanks Joost!

  6. Deryk WenausBy Deryk Wenaus on 9 February, 2012

    If only you could use gravity forms to edit an existing post in the front end, that would be incredibly useful.

    • AntonyBy Antony on 9 February, 2012

      Hard to do the edit if the user is not logged on.
      You can use the front end editor plugin on wp.org to handle editing as an admin quickly.

    • Roger CoathupBy Roger Coathup on 23 February, 2012

      We have editing working with Gravity Forms, through a fairly simple process:
      1. Embed a hidden field in your form to hold the ID of the post to edit
      2. When you display the form using gravity_form() function in your template, pre-populate the fields with the data from your post ($field_values parameter)
      3. Add a hook to gform_post_data, that extracts the ID from the form (rgpost() function) and set $post_data["ID"] = your_post_id;

      That’s it – when GF calls the wp_insert_post it will detect that ID is set and modify the existing post, rather than creating a new one.

      • Ann BannisterBy Ann Bannister on 5 March, 2012

        Hi Roger, I am fairly new to Gravity Forms and have been searching for the instructions on how to add a hook to extract information submitted from a list of categories. I do not even know how to add a hook. Can you point me in the right direction?

  7. KerriBy Kerri on 9 February, 2012

    Thank you Joost ! I’ve been thinking of purchasing Gravity Forms for a while now and your post has helped tremendously. I appreciate all you contribute to the WP community and will be sure to use your link when it’s time to buy.

    Kind Regards,
    Kerri

  8. MicheleBy Michele on 10 February, 2012

    Joost,
    Fantastic article!! I cannot thank you enough for turning me on to Gravity Forms it is an amazing plugin (just like yours are).

    Thank you so much again for all that you do and write I have learned so much from you.

    Warmest Regards,
    Michele

  9. Louis_DeaBy Louis_Dea on 10 February, 2012

    The plugin is awesome. I also add the plugin : “Gravity Forms + Custom Post Types” which add the possibility to add also custom taxonomies which is totally awesome!

  10. Doug SmithBy Doug Smith on 14 February, 2012

    Gravity Froms and the Custom Post Types plugin have been a fantastic combination for one of the sites I run. We’ve been adding more user submitted features and it works so well to collect that information and keep it for our approval before making it live on the site.

    The one thing I don’t like is that Gravity Forms also stores a record for the entry. Any ideas on how to make it stop doing that when the data is being store in a custom post type?

    • SimonBy Simon on 14 February, 2012

      Hi Doug – that’s what I also needed to do, I’ve put the code up on Pastebin (originally found via the Gravity Help forums): http://pastebin.com/hfNWK5U9

      • Doug SmithBy Doug Smith on 14 February, 2012

        Thanks. That did the trick after I fixed one error in the code. There is an extra right parenthesis in the add_action line. It should read:

        add_action( 'gform_post_submission', 'my_remove_entries', 10, 2 );

        • Ken SnyderBy Ken Snyder on 3 March, 2012

          What are the 10 and 2 references too? Does this block all custom post submissions but leaves other submissions?

  11. Ed CoyneBy Ed Coyne on 15 February, 2012

    Ha! I read your to-do list and the first thing that came to my mind was “VIEWS!” Then I read the next sentence.

  12. MackeBy Macke on 17 February, 2012

    Hey, great post but I’m having a problem understanding how to display custom post types in my posts. I’m making an ad site but it’s only the body and title showing up in posts.

  13. MeganBy Megan on 20 February, 2012

    We LOVE gravity forms! Have been using on them on our website recently for a sign up form. Had to get a programmer to set them up for us but the results are amazing

  14. Dave PorterBy Dave Porter on 23 February, 2012

    Really looking forward to the final installment of these articles ? It’s been a while! Hope you have not forgotten?
    Cheers, Dave

  15. Tony LimaBy Tony Lima on 23 February, 2012

    I agree with Dave Porter — eagerly awaiting the third installment!

  16. GordonBy Gordon on 23 February, 2012

    Me too. Will the final solution include search within the custom posts? Will you be using a WP archive of those posts or something else?

  17. DavidBy David on 23 February, 2012

    Love this plugin and this helpful write-up.

    I am a little confused regarding posting data to custom fields. Should a list of available custom fields show up under “Custom Field Name – Existing”? I have created custom fields – they show up on the WP editor for the CPT but nothing shows up in this plugin.

    Or no I need to enter them under “New” before they will show up? If I have to enter them, and I have added them to a Custom Field Group, it sounds like I would enter them without a “wpcf” prefix?

  18. Michael GenelesBy Michael Geneles on 25 February, 2012

    Great writeup. When will you publish Part 3?

  19. Scottie MaymanBy Scottie Mayman on 27 February, 2012

    “Creating a browsable interface for this post type.”

    I’m pretty sure where you’re going, but I’d like to see the route you chose.

  20. CarlosBy Carlos on 28 February, 2012

    LOVE IT. any way to integrate this with your SEO plugin? would be great to allow all the SEO options per page/post be available via the form.

  21. CarlosBy Carlos on 2 March, 2012

    I’ve been digging away at this and trying to figure out if it is possible to add the SEO fields to one of these forms? Anyone have any thoughts?

  22. Ken SnyderBy Ken Snyder on 3 March, 2012

    Great plugin and great write up. I’m left with one questions that I haven’t been able to answer for myself:

    1. Is there a way to get the list of possible Select values from a Custom Field (which is defined as a Select Field) and into a select box form? I’m probably just being dumb but can’t find a way to get this to work.

    I’m also quite interested to hear more from Roger on his “update” solution. Sounds like he’s got it solved but I’m a little too much of a beginner to understand his solution.

  23. Robert JongkeesBy Robert Jongkees on 4 March, 2012

    You didn’t create page 3 yet?
    >> 03. Creating a browsable interface for this post type.
    Tried to find it via the search, but seems its not there yet. Would like to learn some more from your best practices etc.