Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sarastanger/b00fccdad99d6df8d711fb20396e5d1e to your computer and use it in GitHub Desktop.
Save sarastanger/b00fccdad99d6df8d711fb20396e5d1e to your computer and use it in GitHub Desktop.
Hooking up three different Gravity forms to feed The Events Calendar custom post type: add venue and organizer information, add featured image support. Adaptation of: https://gist.github.com/leepettijohn/0ab7e5a7474f9f77df20#file-functions-php-L170 by @leepettijohn
<?php
function my_theme_enqueue_styles() {
$parent_style = 'parent-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style )
);
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
// 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_1', 'populate_venues' );
add_filter( 'gform_pre_validation_1', 'populate_venues' );
add_filter( 'gform_pre_submission_filter_1', 'populate_venues' );
add_filter( 'gform_admin_pre_render_1', '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;
}
add_filter( 'gform_pre_render_2', 'populate_venues2' );
add_filter( 'gform_pre_validation_2', 'populate_venues2' );
add_filter( 'gform_pre_submission_filter_2', 'populate_venues2' );
add_filter( 'gform_admin_pre_render_2', 'populate_venues2' );
function populate_venues2( $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;
}
add_filter( 'gform_pre_render_3', 'populate_venues3' );
add_filter( 'gform_pre_validation_3', 'populate_venues3' );
add_filter( 'gform_pre_submission_filter_3', 'populate_venues3' );
add_filter( 'gform_admin_pre_render_3', 'populate_venues3' );
function populate_venues3( $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_1', 'populate_organizers' );
add_filter( 'gform_pre_validation_1', 'populate_organizers' );
add_filter( 'gform_pre_submission_filter_1', 'populate_organizers' );
add_filter( 'gform_admin_pre_render_1', '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 organizers instead of venues
add_filter( 'gform_pre_render_2', 'populate_organizers2' );
add_filter( 'gform_pre_validation_2', 'populate_organizers2' );
add_filter( 'gform_pre_submission_filter_2', 'populate_organizers2' );
add_filter( 'gform_admin_pre_render_2', 'populate_organizers2' );
function populate_organizers2( $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 organizers instead of venues
add_filter( 'gform_pre_render_3', 'populate_organizers3' );
add_filter( 'gform_pre_validation_3', 'populate_organizers3' );
add_filter( 'gform_pre_submission_filter_3', 'populate_organizers3' );
add_filter( 'gform_admin_pre_render_3', 'populate_organizers3' );
function populate_organizers3( $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_1', 'populate_categories' );
add_filter( 'gform_pre_validation_1', 'populate_categories' );
add_filter( 'gform_pre_submission_filter_1', 'populate_categories' );
add_filter( 'gform_admin_pre_render_1', '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;
}
//This section mirrors the previous to get categories
add_filter( 'gform_pre_render_2', 'populate_categories2' );
add_filter( 'gform_pre_validation_2', 'populate_categories2' );
add_filter( 'gform_pre_submission_filter_2', 'populate_categories2' );
add_filter( 'gform_admin_pre_render_2', 'populate_categories2' );
function populate_categories2( $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;
}
//This section mirrors the previous to get categories
add_filter( 'gform_pre_render_3', 'populate_categories3' );
add_filter( 'gform_pre_validation_3', 'populate_categories3' );
add_filter( 'gform_pre_submission_filter_3', 'populate_categories3' );
add_filter( 'gform_admin_pre_render_3', 'populate_categories3' );
function populate_categories3( $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_1", "format_event_date");
function format_event_date($form){
//set all your field parameters here
$formEventTitle = 1;
$formEventDescription = 10;
$formEventDate = 8;
$formEventStart = 11;
$formEventEnd = 7;
//$formVenueId = 9;
//$formOrganizerId = 10;
//$formURL = 13;
//$formCategory = 12;
$formCost = 12;
//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);
}
add_action("gform_pre_submission_2", "format_event_date2");
function format_event_date2($form){
//set all your field parameters here
$formEventTitle = 15;
$formEventDescription = 29;
$formEventDate = 8;
$formEventStart = 11;
$formEventEnd = 7;
//$formVenueId = 9;
//$formOrganizerId = 10;
$formURL = 21;
//$formCategory = 12;
//$formCost = 33;
$formEventVenue = 17;
$formEventCity = 32;
$formEventPhone = 30;
$formEventOrganizer = 29;
$formOrganizerEmail = 27;
//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' => array(
'Venue' => $_POST['input_'. $formEventVenue],
'City' => $_POST['input_'. $formEventCity],
'Phone' => $_POST['input_'. $formEventPhone]
),
'Organizer' => array(
'Organizer' => $_POST['input_'. $formEventOrganizer],
'Email' => $_POST['input_'. $formOrganizerEmail]
),
'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);
}
add_action("gform_pre_submission_3", "format_event_date3");
function format_event_date3($form){
//set all your field parameters here
$formEventTitle = 15;
$formEventDescription = 29;
$formEventDate = 8;
$formEventStart = 11;
$formEventEnd = 7;
//$formVenueId = 9;
//$formOrganizerId = 10;
$formURL = 21;
//$formCategory = 12;
$formCost = 33;
$formEventVenue = 17;
$formEventAddress = 34;
$formEventCity = 35;
$formEventZip = 36;
$formEventPhone = 32;
$formEventOrganizer = 31;
$formOrganizerEmail = 27;
//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],
'EventShowMapLink' => true,
'EventShowMap' => true,
//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' => array(
'Venue' => $_POST['input_'. $formEventVenue],
'Country' => 'US',
'Address' => $_POST['input_'. $formEventAddress],
'City' => $_POST['input_'. $formEventCity],
'State' => 'CA',
'Zip' => $_POST['input_'. $formEventZip],
'Phone' => $_POST['input_'. $formEventPhone]
),
'Organizer' => array(
'Organizer' => $_POST['input_'. $formEventOrganizer],
'Email' => $_POST['input_'. $formOrganizerEmail]
),
'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_1", "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[5])){
$url = $entry[5];
require_once(ABSPATH . 'wp-admin' . '/includes/image.php');
require_once(ABSPATH . 'wp-admin' . '/includes/file.php');
require_once(ABSPATH . 'wp-admin' . '/includes/media.php');
//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 will add the image uploaded in the form to the Featured Image in the event
add_action("gform_post_submission_2", "add_event_featured_image2");
function add_event_featured_image2($entry){
//get the URL of the image that was uploaded in the gravity forms upload folder
if( !empty($entry[31])){
$url = $entry[31];
require_once(ABSPATH . 'wp-admin' . '/includes/image.php');
require_once(ABSPATH . 'wp-admin' . '/includes/file.php');
require_once(ABSPATH . 'wp-admin' . '/includes/media.php');
//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 will add the image uploaded in the form to the Featured Image in the event
add_action("gform_post_submission_3", "add_event_featured_image3");
function add_event_featured_image3($entry){
//get the URL of the image that was uploaded in the gravity forms upload folder
if( !empty($entry[30])){
$url = $entry[30];
require_once(ABSPATH . 'wp-admin' . '/includes/image.php');
require_once(ABSPATH . 'wp-admin' . '/includes/file.php');
require_once(ABSPATH . 'wp-admin' . '/includes/media.php');
//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