Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Add Event to The Events Calendar from a Gravity Form
//The following section is an add-on to this tutorial - https://tri.be/gravity-forms-events-calendar-submissions/
//Shout to CreativeSlice.com for their initial work
/* Before Starting:
- Make sure you have these three plugins installed
- Gravity Forms
- The Events Calendar
- Gravity Forms + Custom Post Types
- Once Gravity Forms is installed, create a form with these fields
- Single Line Text (Event Title)
- Paragraph Text (Event Description)
- Date (Date of Event)
- Time (Start Time)
- Time (End Time)
- Dropdown (Choose a Venue - CSS Class = venue_choice)
- Dropdown (Choose an Organizer - CSS Class = organizer_choice)
- Dropdown (Choose a Category - CSS Class = category_choice)
- NOTE: Still conversing with The Events Calendar staff to make this functionality work
- Image (Featured Image Upload)
- Website (Event URL)
- Number (Cost for Event)
// Use this section to populate the Venue choices in the dropdown
// Shout to https://www.gravityhelp.com/documentation/article/dynamically-populating-drop-down-fields/
// Change "34" to your form ID
add_filter( 'gform_pre_render_34', 'populate_venues' );
add_filter( 'gform_pre_validation_34', 'populate_venues' );
add_filter( 'gform_pre_submission_filter_34', 'populate_venues' );
add_filter( 'gform_admin_pre_render_34', 'populate_venues' );
function populate_venues( $form ) {
foreach ( $form['fields'] as &$field ) {
// you can choose different parameters to select the correct field. I used the class for this example
if ( $field->type != 'select' || strpos( $field->cssClass, 'venue_choice' ) === false ) {
continue;
}
// you can add additional parameters here to alter the posts that are retrieved
// more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
$posts = get_posts( 'numberposts=-1&post_type=tribe_venue&orderby=title&order=ASC' );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
$field->placeholder = 'Click to Select a Venue';
$field->choices = $choices;
}
return $form;
}
//This section mirrors the previous to get organizers instead of venues
add_filter( 'gform_pre_render_34', 'populate_organizers' );
add_filter( 'gform_pre_validation_34', 'populate_organizers' );
add_filter( 'gform_pre_submission_filter_34', 'populate_organizers' );
add_filter( 'gform_admin_pre_render_34', 'populate_organizers' );
function populate_organizers( $form ) {
foreach ( $form['fields'] as &$field ) {
// you can choose different parameters to select the correct field. I used the class for this example
if ( $field->type != 'select' || strpos( $field->cssClass, 'organizer_choice' ) === false ) {
continue;
}
// you can add additional parameters here to alter the posts that are retrieved
// more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
$posts = get_posts( 'numberposts=-1&post_type=tribe_organizer&orderby=title&order=ASC' );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
$field->placeholder = 'Click to Select an Organizer';
$field->choices = $choices;
}
return $form;
}
//This section mirrors the previous to get categories
add_filter( 'gform_pre_render_34', 'populate_categories' );
add_filter( 'gform_pre_validation_34', 'populate_categories' );
add_filter( 'gform_pre_submission_filter_34', 'populate_categories' );
add_filter( 'gform_admin_pre_render_34', 'populate_categories' );
function populate_categories( $form ) {
foreach ( $form['fields'] as &$field ) {
// you can choose different parameters to select the correct field. I used the class for this example
if ( $field->type != 'select' || strpos( $field->cssClass, 'category_choice' ) === false ) {
continue;
}
// you can add additional parameters here to alter the posts that are retrieved
$taxtype = array('tribe_events_cat');
$args = array(
'orderby'=> 'name',
'order' => 'ASC'
);
$taxs = get_terms($taxtype,$args);
$choices = array();
foreach ( $taxs as $tax ) {
$choices[] = array( 'text' => $tax->name, 'value' => $tax->name );
}
$field->placeholder = 'Click to Select a Category';
$field->choices = $choices;
}
return $form;
}
add_action("gform_pre_submission_34", "format_event_date");
function format_event_date($form){
//set all your field parameters here
$formEventTitle = 7;
$formEventDescription = 8;
$formEventDate = 3;
$formEventStart = 4;
$formEventEnd = 5;
$formVenueId = 9;
$formOrganizerId = 10;
$formURL = 13;
$formCategory = 12;
$formCost = 14;
//Getting the Category ID
$formCategoryName = $_POST['input_'. $formCategory];
$formCategoryObject = get_term_by('name',$formCategoryName,'tribe_events_cat');
$formCategoryID = intval($formCategoryObject->term_id);
//Set the Venue and Organizer Names
$venueinput = $_POST['input_'. $formVenueId];
$organizerinput = $_POST['input_'. $formOrganizerId];
//Set the Venue ID from the name
$venueid = get_post_by_title($venueinput,'tribe_venue');
if (is_numeric($venueid)){$venuesubmit = array('VenueID' => $venueid);} else {$venuesubmit = ''; }
//Set the Organizer ID from the name
$organizerid = get_post_by_title($organizerinput,'tribe_organizer');
if (is_numeric($organizerid)){$organizersubmit = array('OrganizerID' => $organizerid);} else {$organizersubmit = '';};
// Shout to this URL as an example - https://theeventscalendar.com/support/forums/topic/example-use-of-tribe_create_event/
$my_post = array(
//'post_title' => $formCategoryID,
'post_title' => $_POST['input_'. $formEventTitle],
'post_content' => $_POST['input_'. $formEventDescription],
//Still trying to figure out how to get the post_category to work
//'post_category' => array(6),
'EventStartDate' => $_POST['input_'. $formEventDate],
'EventCost' => $_POST['input_'. $formCost],
'Venue' => $venuesubmit,
'Organizer' => $organizersubmit,
'EventURL' => $_POST['input_'. $formURL]
);
if ($_POST['input_'. $formEventStart][0] != 0){
$my_post['EventStartHour'] = $_POST['input_'. $formEventStart][0];
$my_post['EventStartMinute'] = $_POST['input_'. $formEventStart][1];
$my_post['EventStartMeridian'] = $_POST['input_'. $formEventStart][2];
$my_post['EventEndHour'] = $_POST['input_'. $formEventEnd][0];
$my_post['EventEndMinute'] = $_POST['input_'. $formEventEnd][1];
$my_post['EventEndMeridian'] = $_POST['input_'. $formEventEnd][2];
$my_post['EventEndDate'] = $_POST['input_'. $formEventDate];
}
//NOTE: You must submit the start hour and minute so I added this so that I could be notified when someone didn't submit a start hour
//Shout to MyRestlessDream for noticing this - https://wordpress.org/support/topic/tribe_create_event-problem-with-the-date
else {
$my_post['EventStartHour'] = '17';
$my_post['EventStartMinute'] = '0';
$my_post['post_content'] = $_POST['input_'. $formEventDescription]. '<br /> ALL DAY EVENT';
}
//This is the function that will allow you to create a new event
// Shout to https://theeventscalendar.com/function/tribe_create_event/
$neweventid = tribe_create_event($my_post);
}
//This will add the image uploaded in the form to the Featured Image in the event
add_action("gform_post_submission_34", "add_event_featured_image");
function add_event_featured_image($entry){
//get the URL of the image that was uploaded in the gravity forms upload folder
if( !empty($entry[11])){
$url = $entry[11];
//get the latest event that was created
// Shout out to http://wordpress.stackexchange.com/questions/28484/get-most-recent-media-upload
$events = get_posts( array(
'post_type' => 'tribe_events',
'posts_per_page' => 1,
'orderby' => 'ID',
'order' => 'DESC' ,
'post_status' => 'draft'
) );
foreach ( $events as $event ) {
$latesteventid = $event->ID;
}
//Upload the submitted image to the latest event in the Media Library
$image = media_sideload_image($url, $latesteventid);
//Get the latest uploaded image
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => 1,
'post_status' => null,
'post_mime_type' => 'image'
) );
foreach ( $attachments as $attachment ) {
$latestuploadid = $attachment->ID;
}
//Set the image to the latest event Featured Image
update_post_meta($latesteventid, '_thumbnail_id',$latestuploadid);
}
}
//This function is used to get the ID of a specific post type from its name
function get_post_by_title($page_title, $post_type ='post') {
global $wpdb;
$post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $page_title, $post_type));
if ( $post ) return $post;
return $page_title;
}

Hi there leepettijohn! This is fantastic! Thank you so much for all of your work on this function. I was curious to know if you have explored this code any farther or if you could give me any advice on adapting this further to support more fields?

dusauton commented Aug 2, 2016

Hi!
Thanks a lot, this is indeed very useful! However, I have a problem with the 'media_sideload_image' function... it doesn't seem to upload the image from the Gravity temp location :(
If I comment this line ($image = media_sideload_image($url, $latesteventid);)) , then the whole function works properly, i.e. the event gets created successfully.
What I can see is that the latesteventid is retrieved correctly, the image is uploaded sucessfully in the Gravity form temp location.
Would you have any idea why the 'media_sideload_image' function does not work? Is there any way I can debug it?

Thanks! Nicolas

rtandoh commented Aug 21, 2016

Lee,

Nice work but I hit the same issue as like Nicolas (dusauton). Any guidance?

FYI I am using The Events Calendar Pro 4.2.4, Gravity Forms 1.9.4 and Gravity Forms + Custom Post Types 3.1.5.

Thanks,
Richard

Owner

leepettijohn commented Aug 28, 2016

Hi all,
My apologies for not getting back sooner. I wasn't receiving any notifications from github. Will take a look this week and keep you updated.

Cheers

Owner

leepettijohn commented Aug 30, 2016

Hi all (again),
I did some work on this today and now that the event creation function has an all-day parameter I was able to get that functioning.

I tried out the function with regard to the featured image and it worked on my end. Do you have any kind of error message, etc? Thanks.

rtandoh commented Sep 1, 2016

Hi Lee,

Thanks for looking into this. I refreshed the code, but I am still experiencing issues.

I get the following and the form doesn’t display:

Fatal error: Call to undefined function media_sideload_image() in/var/www/vhosts/wps1.ismysite.co.uk/httpdocs/wp-content/themes/vivacity/functions.php on line 97

When I comment out $image = media_sideload_image($url, $latesteventid); the form displays.

However, on submission, I get the following errors and the event details though submitted do not appear on The Event Calendar:

Notice: Trying to get property of non-object in /var/www/vhosts/wps1.ismysite.co.uk/httpdocs/wp-content/themes/vivacity/functions.php on line 903 [which relates to $formCategoryID = intval($formCategoryObject->term_id);]

Notice: Undefined index: input_9 in /var/www/vhosts/wps1.ismysite.co.uk/httpdocs/wp-content/themes/vivacity/functions.php on line 906 [which relates to $venueinput = $_POST['input_'. $formVenueId];]

Notice: Undefined index: input_7 in /var/www/vhosts/wps1.ismysite.co.uk/httpdocs/wp-content/themes/vivacity/functions.php on line 920 [which relates to 'post_title' => $_POST['input_'. $formEventTitle],]

Notice: Undefined index: input_8 in /var/www/vhosts/wps1.ismysite.co.uk/httpdocs/wp-content/themes/vivacity/functions.php on line 921 [which related to 'post_content' => $_POST['input_'. $formEventDescription],]

Notice: Undefined index: input_14 in /var/www/vhosts/wps1.ismysite.co.uk/httpdocs/wp-content/themes/vivacity/functions.php on line 925 [which relates to 'EventCost' => $_POST['input_'. $formCost],]

I am using The Events Calendar PRO 4.2.4, Gravity Forms 2.0.6 and Gravity Forms + Custom Post Types 3.1.5.

Cheers,
rt.

This is fantastic work, Lee - kudos! I too am having issues with the featured image, as well as the date of the event. It appears that the form is injecting the last image uploaded in the media library. Also, each time I submit an event, it posts it as the current date. Please advise!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment