I've got this code to work for Gravity Forms v1.3 and The Events Calendar. At the end I've also got a snippet to format the post_name from the post_title for the pretty URL. Also, my date and times are both in an array because i'm using the Date Field rather than the Date Picker in Gravity Forms.
//GravityForms Modifications
//change post type for Events to sp_events for Event Posting form
add_filter("gform_post_data", "change_post_type", 10, 2);
function change_post_type($post_data, $form){
if($form["id"] == '5'){
$post_data["post_type"] = "sp_events";
}
return $post_data;
}
//convert date and time to Events Calendar style for Event Posting form
add_action("gform_pre_submission", "format_event_date");
function format_event_date($form){
//replace 5 with your actual form id
if($form["id"] != 5)
return;
//replace input_5 and input_6 with your actual date field IDs
$startDate = $_POST["input_5"];
$endDate = $_POST["input_6"];
//replace input_23 and input_24 with your actual time field IDs
$startTime = $_POST["input_23"];
$endTime = $_POST["input_24"];
$eventStartDate = $startDate[2] . "-" . $startDate[0] . "-" . $startDate[1] . " " . $startTime[0] . ":" . $startTime[1].$startTime[2];
$eventEndDate = $endDate[2] . "-" . $endDate[0] . "-" . $endDate[1] . " " . $endTime[0] . ":" . $endTime[1].$endTime[2];
$eventStartDate = strtotime($eventStartDate);
$eventEndDate = strtotime($eventEndDate);
//formatting date
$formatted_start_date = strftime("%Y-%m-%d %H:%M:%S", $eventStartDate);
$formatted_end_date = strftime("%Y-%m-%d %H:%M:%S", $eventEndDate);
//replace with the IDs of the hidden fields that will hold the formatted dates
$_POST["input_5"] = $formatted_start_date;
$_POST["input_6"] = $formatted_end_date;
}
add_action("gform_post_submission", "add_EventDatesAndTimes", 10, 2);
function add_EventDatesAndTimes($entry, $form) {
update_post_meta($entry['post_id'], "_EventStartDate", $_POST["input_5"]);
update_post_meta($entry['post_id'], "_EventEndDate", $_POST["input_6"]);
}
add_action("gform_pre_submission", "create_friendly_url");
function create_friendly_url()
{
$phrase = $_POST['title'];
$maxLength = 200;
$result = strtolower($phrase);
$result = preg_replace("/[^a-z0-9\s-]/", "", $result);
$result = trim(preg_replace("/[\s-]+/", " ", $result));
$result = trim(substr($result, 0, $maxLength));
$result = preg_replace("/\s/", "-", $result);
$_POST["postname"] = $formatted_dateend;
}
Posted 14 years ago on Thursday September 16, 2010 |
Permalink