Forked from stephenh1988/front-end-event-creation.php
Last active
February 3, 2018 19:48
-
-
Save stephenharris/4988325 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* This is a basic example of implementing front end creation of events with Event Organiser | |
* @see http://www.stephenharris.info/2012/front-end-event-posting/ | |
* @see http://wordpress.org/extend/plugins/event-organiser/ | |
* | |
*/ | |
add_shortcode('my_event_form','my_event_form_shortcode_handler'); | |
function my_event_form_shortcode_handler( ){ | |
if( !is_user_logged_in() ){ | |
return '<p> Only logged in users can post events </p>'; | |
} | |
$html = '<form method="POST">'; | |
//Create hidden 'action' field and corresponding nonce | |
$html .= '<input type="hidden" name="my-action" value="post-event" >'; | |
$html .=wp_nonce_field( 'post-event', '_mynonce',false,false); | |
//Event Title | |
$html .= sprintf('<p><label for="my-frontend-event-%1$s"> %2$s | |
<input type="text" name="my_frontend_event[%1$s]" id="my-frontend-event-%1$s" > | |
</label></p>', | |
'title', | |
'Event Title' | |
); | |
//Event Description | |
$html .= sprintf('<p><label for="my-frontend-event-%1$s"> %2$s | |
<textarea name="my_frontend_event[%1$s]" id="my-frontend-event-%1$s"></textarea> | |
</label></p>', | |
'description', | |
'Event Description' | |
); | |
//Start date | |
$html .= sprintf('<p><label for="my-frontend-event-%1$s"> %2$s | |
<input type="text" name="my_frontend_event[%1$s]" id="my-frontend-event-%1$s" class="my-frontend-event-datepicker" > | |
</label></p>', | |
'startdate', | |
'Start Date' | |
); | |
//End date | |
$html .= sprintf('<p><label for="my-frontend-event-%1$s"> %2$s | |
<input type="text" name="my_frontend_event[%1$s]" id="my-frontend-event-%1$s" class="my-frontend-event-datepicker" > | |
</label></p>', | |
'enddate', | |
'End Date' | |
); | |
//Venue | |
$html .= sprintf('<p><label for="my-frontend-event-%1$s"> %2$s %3$s </label></p>', | |
'venue', | |
'Venue', | |
wp_dropdown_categories(array( | |
'orderby' => 'ID', | |
'order' => 'ASC', | |
'hide_empty' => 0, | |
'echo' => 0, | |
'id'=>'my-frontend-event-venue', | |
'name' => 'my_frontend_event[venue]', | |
'taxonomy' => 'event-venue' | |
)) | |
); | |
//Category - checklist | |
$cats = get_terms('event-category',array('hide_empty'=>0)); | |
if( $cats ){ | |
$html .= '<p><label for="my-frontend-event-category">Category <ul>'; | |
foreach ( $cats as $cat ){ | |
$html .= sprintf('<li><label for="my-frontend-event-%1$s-%2$d"> | |
<input type="checkbox" name="my_frontend_event[%1$s][]" value="%2$s" id="my-frontend-event-%1$s-%2$d" > | |
%3$s | |
</label></li>', | |
'category', | |
$cat->term_id, | |
esc_html($cat->name) | |
); | |
} | |
$html .= '</label></p>'; | |
} | |
//The post button | |
$html .= '<p><input name="submit" type="submit" id="submit" value="Post Event"></p>'; | |
$html .='</form>'; | |
return $html; | |
} | |
add_action('init','my_frontend_event_post_listner'); | |
function my_frontend_event_post_listner(){ | |
if( !isset($_POST['my-action']) || 'post-event' != $_POST['my-action'] ) | |
return; | |
//Collect raw input | |
$input = $_POST['my_frontend_event']; | |
//Check user is logged in: | |
if( !is_user_logged_in() ) | |
return; | |
//Check nonce | |
check_admin_referer( 'post-event', '_mynonce'); | |
/* | |
* IMPORTANT: Perform any other checks you need to here (e.g. should only users with a certain capability be able to post events?) | |
*/ | |
/** | |
* Set the post data (see http://codex.wordpress.org/Function_Reference/wp_insert_post) | |
* This includes event-category and event-venue (taxonomy terms) | |
*/ | |
//Event venue is just an ID | |
$event_venue = isset($input['venue']) ? (int) $input['venue'] : 0; | |
//Event cats are an array of IDs | |
$event_cats = !empty( $input['category'] ) ? $input['category'] : array(); | |
$event_cats = array_map( 'intval', $event_cats ); | |
$post_data =array( | |
'post_title'=>$input['title'], | |
'post_content'=>$input['description'], | |
'tax_input'=>array( | |
'event-venue'=>array($event_venue), | |
'event-category'=>$event_cats, | |
) | |
); | |
/** | |
* Set the event data | |
*/ | |
//Start and end dates need to be given as DateTime objects (timezone is UTC unless a timezone is given) | |
$start = new DateTime($input['startdate'],eo_get_blog_timezone()); | |
$end = new DateTime($input['enddate'],eo_get_blog_timezone()); | |
$event_data =array( | |
'schedule' =>'once', //specifies the reoccurrence pattern | |
'all_day' => 1, //1 if its an all day event, 0 if not - if not you'll need to specify a start/end time for the DateTimeobjects | |
'start' => $start, //start date (of first occurrence) as a datetime object | |
'end' => $end, //end date (of first occurrence) as a datetime object | |
); | |
//Finally, Insert event. | |
$post_id = eo_insert_event($post_data,$event_data); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @toitrinh can you post your code ?