-
-
Save leepettijohn/0ab7e5a7474f9f77df20 to your computer and use it in GitHub Desktop.
//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; | |
} |
Below tribe_create_event you can add this
wp_set_post_terms( $neweventid, $formCategory, 'tribe_events_cat' );
To set your categories.
Another thing I suggest is to use a hidden field that you can store your event ID in.
$_POST['input_16'] = $neweventid;
You would just change that 16 to the Field ID of your hidden field. Then for your image field you can just use $entry[16] to grab it and avoid doing unnecessary queries. That would go right below the category line above.
For the categories you can add : 'hide_empty' => false in the query :
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',
'hide_empty' => false,
);
$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;
}
You want that people can submit an event in an empty category :)
For the categories, here a solution :
$my_post = array(
'post_title' => $_POST['input_'. $formEventTitle],
'post_status' => 'pending',
'post_content' => $_POST['input_'. $formEventDescription],
'EventStartDate' => $_POST['input_'. $formStartDate],
'EventCost' => $_POST['input_'. $formCost],
'Venue' => $venuesubmit,
'Organizer' => $organizersubmit,
'EventURL' => $_POST['input_'. $formURL],
'EventShowMap' => 'true',
//HERE THE ADDED CODE
'tax_input' => array(
'tribe_events_cat' => $formCategoryID,
),
);
Not sure if anyone still monitors this, but had a question on the "venue/organizer" dropdown.
We want to have a form that the user can enter in their name (the organizer) the location (venue name) and have preset categories. This appears to be grabbing existing venues/organizers in ECP. Is it not possible to send these as new items (single line text)?
We originally tried to do this with the Gravity Form example here: https://docs.gravityforms.com/calendar-events-webhooks-addon-wp-restapi/ but this does not seem to have an option for venue/organizer that we can figure out.
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
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:
// Include media files
if ( !function_exists( 'media_sideload_image' ) ) {
require_once( ABSPATH . 'wp-admin/includes/media.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
}
// Upload the submitted image to the latest event in the Media Library
if ( media_sideload_image( $featured_image_url, $event_id ) ) {
// Get the latest uploaded image
$attachments = get_posts( [
'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( $event_id, '_thumbnail_id', $latestuploadid );
}
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.
Lee,
Here's the fix for categories: