Skip to content

Instantly share code, notes, and snippets.

@stephenh1988
Created October 10, 2012 17:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stephenh1988/3867194 to your computer and use it in GitHub Desktop.
Save stephenh1988/3867194 to your computer and use it in GitHub Desktop.
Front-end event creation with Event Organiser
<?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 = (int) isset($input['venue']) ? $input['venue'] : 0;
//Event cats are an array of IDs
$event_cats = isset($input['category']) ? $input['category'] : array();
$event_cats = array_map('intval',$input['category']);
$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);
}
?>
@HandHugs
Copy link

HandHugs commented Jan 4, 2014

This all works great except for the venues for me - when they click to post the event, it creates a new venue with no information except a title that is the ID of the venue that was selected when they submitted the event, rather than matching them up and using the venue they selected.

The venue is also a little limiting since they cannot add new ones, it might be nice to have an option for just "none" so they aren't forced to pick a venue that is incorrect.

@stephenharris
Copy link

Hi @HandHugs this gist is now being maintained here: https://gist.github.com/stephenharris/4988325 (which has now been corrected). I'll update the tutorial too.

It's entirely possible to select 'no venue' - see the codex for the wp_dropdown_categories() function used to produce the drop-down menu of venues. Specifically the show_option_none argument.

As for creating new venues, this is entirely possible to (see the documentation of eo_insert_venue(): http://codex.wp-event-organiser.com/function-eo_insert_venue.html). This was just a simple example to demonstrate what you can do.

The front-end submission extension shall include the ability to accept new venues. You'll be able to create your own form by adding various fields (event name, date, recurrence schedule, venues, categories and other fields for meta data) as desired and then use drag-and-drop to order them as required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment