Getting Stuff Out of Gravity Forms

I recently worked on a project that used Gravity Forms to collect data from an end user, and then rather than just putting the data in an email (which is the default setting), but create a post from a custom post type.

Create the Custom Post Type

First create a custom post type. Official instructions here.

My custom post type code:

function thing_register_cpt() {
	$args = array(
		'labels' => array(
				'name' => 'Thing',
				'singular_name' => 'Thing',
				'all_items' => 'All Thing',
				'add_new' => 'Add New Thing',
				'add_new_item' => 'Add New Thing',
				'edit_item' => 'Edit Thing',
				'new_item' => 'Add New Thing',
				'view_item' => 'View Thing',
				'search_items' => 'Search Thing',
				'not_found' => 'No Thing Found',
				'not_found_in_trash' => 'No Thing Found in Trash'
			'public' => true,
			'publicly_queryable' => false,
			'has_archive' => false,
			'menu_position' => 5,
			'query_var'    => true,
			'supports' => array(


	register_post_type('omc_thing', $args);

Don’t forget to register your custom post type.

add_action('init', 'thing_register_cpt');

Create the Meta Boxes (Fields)

Then, you create a meta_box (what WordPress calls field) for each input that you want to capture from the Gravity Form.

function listing_metaboxes( array $meta_boxes ) {

	// Start with an underscore to hide fields from custom fields list
	$prefix = '_sitemeta_';

        $meta_boxes[] = array(
		'id'         => 'Thing-meta', //ID of the metabox
		'title'      => 'Thing Title', //Title that will appear in editor
		'pages'      => array( 'omc_thing'), // Custom post type
		'context'    => 'normal', //Main column in the editor
		'priority'   => 'high', //Priority..
		'show_names' => true, // Show field names on the left
		'fields'     => array(
				'name' => 'Name of Thing',
				'desc' => 'List the name of this Thing',
				'id' => $prefix . 'omc_name_of_thing',
				'type' => 'text_medium',	
				'name' => 'Color of Thing',
				'desc' => 'List the color of the thing',
				'id' => $prefix . 'omc_color_of_thing',
				'type' => 'date',
         return $meta_boxes;

Hook it Up

After the post type is created and all meta_boxes (fields) have been added to said post type, it’s time to hook it into the lifecycle of Gravity Forms. More information about the gform_after_submission function can be found here. The number _1 on the end of the function tells Gravity Forms that it is receiving data from the Form with an ID of 1.

add_action('gform_after_submission_1', "create_thing_from_submission", 10, 2);

Lastly, create a function that creates a Thing on submission of the form.

function create_thing_from_submission($entry, $form){
	$new_thing = array(
		'post_title'	=>	ucwords($entry['92.2']), //enter field ID generated by Form 
		'post_status'	=>	'publish',
		'post_type'		=>	'en_thing'

	$theID = wp_insert_post($new_thing);
	$thePrefix = '_sitemeta_';
	update_post_meta($theID, $thePrefix . "omc_name_of_thing", ucwords($entry['92.2']));
	update_post_meta($theID, $thePrefix . "omc_color_of_thing", ucwords($entry['3']));

Next I will explain how I then displayed the captured data in the Editor so that it can be changed from the Admin panel.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *