Last active
June 28, 2024 18:24
-
-
Save leepettijohn/0ab7e5a7474f9f77df20 to your computer and use it in GitHub Desktop.
Add Event to The Events Calendar from a Gravity Form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I came across the same thing just now, but found in the docs for
media_sideload_image()
that you need to include the required files. This worked for me:By the way, @leepettijohn, I set my whole form up on my own and got stuck on the featured image. Your code is very similar to mine. :) Thanks for the idea on uploading the image to the media library and then retrieving it.