Event Espresso Featured Images
Upload a Featured Image Using a WordPress Meta Box
This process seemed pretty straight-forward on its face. My original mission was to add the ability for a client to upload a featured image attached to an event in a heavily customized version of the Event Espresso plugin. Without engaging in what would be a completely exhaustive account of the idiosyncrasies inherent in Event Espresso itself, here is what finally worked for me.
I totally got bailed out by Matt at Webmaster-Source and by Austin Passy. A ton of people online have created tutorials for doing something similar, but Event Espresso posed inherent challenges by not relying on WordPress-standard implementations of things like meta boxes (or of most things I encountered for that matter).
The Code
This is the code I needed to insert at the top of my administrative edit action page (edit_event.php):
- wp_enqueue_script('media-upload');
- wp_enqueue_script('thickbox');
- wp_register_script('my-upload', WP_PLUGIN_URL . '/event-espresso.3.1.15.P/scripts/media-ul.js', array('jquery','media-upload','thickbox'));
- wp_enqueue_script('my-upload');
- wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_register_script('my-upload', WP_PLUGIN_URL . '/event-espresso.3.1.15.P/scripts/media-ul.js', array('jquery','media-upload','thickbox'));
wp_enqueue_script('my-upload');
wp_enqueue_style('thickbox');The media-upload script allows us to call the WordPress media uploader functionality. This is roughly the dialog you see when you set the featured image on a post or page. Also, we want to incorporate the thickbox functionality to enable us to call the media uploader in a nifty popup.
Here’s the content of the media-ul.js file we’re calling above:
- jQuery(document).ready(function() {
- var formfield;
- jQuery('#upload_image_button').click(function() {
- jQuery('html').addClass('Image');
- formfield = jQuery('#upload_image').attr('name');
- tb_show('', 'media-upload.php?type=image&TB_iframe=true');
- return false;
- });
- // we need to catch the output from the media uploader
- // this is a nifty little workaround that allows us to leave the code for the uploader intact
- window.original_send_to_editor = window.send_to_editor;
- window.send_to_editor = function(html){
- if (formfield) {
- // build the fileurl var from the image src of the selected image
- fileurl = jQuery('img',html).attr('src');
- // update the image path form field
- jQuery('#upload_image').val(fileurl);
- // change the hidden field so we can do our db stuff when we hand
- // the form data off for processing
- jQuery('#upload_image_changed').val('1');
- // reveal the div with the image in it
- jQuery('#upload_image_display').show('fast');
- // update the featured image src to show the image
- jQuery('#upload_image_img').attr('src', fileurl);
- // hide the thickbox we instantiated
- tb_remove();
- jQuery('html').removeClass('Image');
- } else {
- window.original_send_to_editor(html);
- }
- };
- });
jQuery(document).ready(function() {
var formfield;
jQuery('#upload_image_button').click(function() {
jQuery('html').addClass('Image');
formfield = jQuery('#upload_image').attr('name');
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
return false;
});
// we need to catch the output from the media uploader
// this is a nifty little workaround that allows us to leave the code for the uploader intact
window.original_send_to_editor = window.send_to_editor;
window.send_to_editor = function(html){
if (formfield) {
// build the fileurl var from the image src of the selected image
fileurl = jQuery('img',html).attr('src');
// update the image path form field
jQuery('#upload_image').val(fileurl);
// change the hidden field so we can do our db stuff when we hand
// the form data off for processing
jQuery('#upload_image_changed').val('1');
// reveal the div with the image in it
jQuery('#upload_image_display').show('fast');
// update the featured image src to show the image
jQuery('#upload_image_img').attr('src', fileurl);
// hide the thickbox we instantiated
tb_remove();
jQuery('html').removeClass('Image');
} else {
window.original_send_to_editor(html);
}
};
});This script fires at document load and attaches the functionality we need to the ‘Update Image’ button and handles the form events handed off by the media uploader.
Further down, inside the edit action page (edit_event.php), I had to write a pseudo-meta-box from scratch to display all of this stuff.
- $feat_img_sql = "SELECT * FROM wp_events_featured_img WHERE event_id = '" . $event_id . "'";
- $feat_img_data = $wpdb->get_row($feat_img_sql);
- $feat_img_inside = "<div class='misc-pub-section'>" . __('Image File', 'event_espresso') . "";
- $feat_img_inside .= "<input type='text' id='upload_image' name='upload_image' value='";
- if (!empty($feat_img_data->img_path)){
- $feat_img_inside .= $feat_img_data->img_path;
- } else {
- $feat_img_inside .= "Click Update Image!";
- }
- $feat_img_inside .= "' style='width:100%' /></div>";
- $feat_img_inside .= "<div id='upload_image_display' class='misc-pub-section misc-pub-section-last'";
- if (empty($feat_img_data->img_path)){
- $feat_img_inside .= " style='display:none'";
- }
- $feat_img_inside .= "><img id='upload_image_img' src='" . $feat_img_data->img_path . "' style='width:258px' /></div>";
- $feat_img_inside .= "<div style='padding:5px 10px'><input type='hidden' name='upload_image_changed' id='upload_image_changed' value='0' />";
- $feat_img_inside .= "<input class='button-primary' id='upload_image_button' type='button' value='Update Image' /></div>";
- ?>
- <!-- BEGIN Featured Image Options -->
- <div id="feat-img" class="postbox">
- <div class="handlediv" title="Click to toggle"></div>
- <h3 class="hndle"><span><?php _e('Featured Image', 'event_espresso'); ?></span></h3>
- <div class="inside" style="padding:0;margin:0">
- <?php echo $feat_img_inside; ?>
- </div>
- </div>
- <!-- END Featured Image Options -->
$feat_img_sql = "SELECT * FROM wp_events_featured_img WHERE event_id = '" . $event_id . "'";
$feat_img_data = $wpdb->get_row($feat_img_sql);
$feat_img_inside = "<div class='misc-pub-section'>" . __('Image File', 'event_espresso') . "";
$feat_img_inside .= "<input type='text' id='upload_image' name='upload_image' value='";
if (!empty($feat_img_data->img_path)){
$feat_img_inside .= $feat_img_data->img_path;
} else {
$feat_img_inside .= "Click Update Image!";
}
$feat_img_inside .= "' style='width:100%' /></div>";
$feat_img_inside .= "<div id='upload_image_display' class='misc-pub-section misc-pub-section-last'";
if (empty($feat_img_data->img_path)){
$feat_img_inside .= " style='display:none'";
}
$feat_img_inside .= "><img id='upload_image_img' src='" . $feat_img_data->img_path . "' style='width:258px' /></div>";
$feat_img_inside .= "<div style='padding:5px 10px'><input type='hidden' name='upload_image_changed' id='upload_image_changed' value='0' />";
$feat_img_inside .= "<input class='button-primary' id='upload_image_button' type='button' value='Update Image' /></div>";
?>
<!-- BEGIN Featured Image Options -->
<div id="feat-img" class="postbox">
<div class="handlediv" title="Click to toggle"></div>
<h3 class="hndle"><span><?php _e('Featured Image', 'event_espresso'); ?></span></h3>
<div class="inside" style="padding:0;margin:0">
<?php echo $feat_img_inside; ?>
</div>
</div>
<!-- END Featured Image Options -->Not my most elegant product, but it works just fine. Now I can treat the basic Event Espresso events a bit more like posts. This gives me the flexibility to incorporate featured images into my templates, which are increasingly taking advantage of the featured image.
Astute readers may also note that I had to create another table for the featured image in the database. It’s a simple table with two columns (event_id, and img_path). This means modifying my database queries to include a JOIN or adding an outboard query anywhere I need to implement the featured image, but I’ve done my best to leave Event Espresso’s original code intact as I consider it to be a museum-grade object lesson in how not to structure a commercial WordPress plugin.





Hi Jonahs,
Just ran across this post while searching for something else. Sorry to hear that adding the metabox and image upload script was such a pain. We have added a featured image upload tool to the Event Espresso admin. The featured images are actually stored in the event meta and can be retrieved in the event query.
Just wanted to let you know. Take care :)
Seth Shoultes
Event Espresso
@SethShoultes Outstanding! Excellent to hear, Seth!
I'll be the first to admit that there are tons of challenges to be overcome in developing any piece of software. It's commendable that you've taken the time here to update my readership on the current state of your product. I'll be happy to review the latest version of Event Espresso whenever a project comes along which presents such an opportunity.
A magazine theme would make ur blog look nicer :)
I'll bite. :) Any designs you like particularly that you could recommend?
didnt know where i should place the last portion of the edit_even.php stuff?
It just needs to be in the sidebar, which is toward the top of the file. I'd recommend placing it after the compulsory update stuff. Search for the '/submitdiv' HTML comment tag. Insert the code right after that.