Skip to content

Instantly share code, notes, and snippets.

@tharmann
Created April 19, 2018 16:02
Show Gist options
  • Select an option

  • Save tharmann/65990558e2201296d09d7c8dfe59d3ef to your computer and use it in GitHub Desktop.

Select an option

Save tharmann/65990558e2201296d09d7c8dfe59d3ef to your computer and use it in GitHub Desktop.
Add upload field (for PDF Flyers attached to the Event) to Tribe Community Events with Events Pro - WordPress
<?php
/**
* Single Event Meta (Additional Fields) Template
*
* Override this template in your own theme by creating a file at:
* [your-theme]/tribe-events/pro/modules/meta/additional-fields.php
*
* @package TribeEventsCalendarPro
*/
if ( ! isset( $fields ) || empty( $fields ) || ! is_array( $fields ) ) {
return;
}
?>
<div class="tribe-events-meta-group tribe-events-meta-group-other">
<h3 class="tribe-events-single-section-title"> <?php esc_html_e( 'Actions', 'tribe-events-calendar-pro' ) ?> </h3>
<dl>
<?php foreach ( $fields as $name => $value ): ?>
<dt> <?php echo esc_html( $name ); ?> </dt>
<dd class="tribe-meta-value">
<?php
// This can hold HTML. The values are cleansed upstream
echo $value;
?>
</dd>
<?php endforeach ?>
</dl>
<!--Here's the code that displays a link to the uploaded flyer when the event is displayed. Using the Events Pro template
for additional fields in this case, but any of the event view templates could really be used if you don't have Pro.-->
<?php $flyer = get_post_meta( get_the_ID(), 'event-flyer', TRUE ); ?>
<?php if ( $flyer ) { ?>
<h3 class="tribe-events-single-section-title">Downloads</h3>
<dl>
<dt>Event Flyer</dt>
<dd>
<a href="<?php echo $flyer; ?>">Download Flyer</a>
</dd>
</dl>
<?php } ?>
</div>
<?php
/**
* Event Submission Form
* The wrapper template for the event submission form.
*
* Override this template in your own theme by creating a file at
* [your-theme]/tribe-events/community/edit-event.php
*
* @since 3.1
* @version 4.5
*
* @var int|string $tribe_event_id
*/
if ( ! defined( 'ABSPATH' ) ) {
die( '-1' );
}
if ( ! isset( $tribe_event_id ) ) {
$tribe_event_id = null;
}
?>
<?php tribe_get_template_part( 'community/modules/header-links' ); ?>
<?php do_action( 'tribe_events_community_form_before_template', $tribe_event_id ); ?>
<form method="post" enctype="multipart/form-data" data-datepicker_format="<?php echo esc_attr( tribe_get_option( 'datepickerFormat', 0 ) ); ?>">
<input type="hidden" name="post_ID" id="post_ID" value="<?php echo absint( $tribe_event_id ); ?>"/>
<?php wp_nonce_field( 'ecp_event_submission' ); ?>
<?php tribe_get_template_part( 'community/modules/title' ); ?>
<?php tribe_get_template_part( 'community/modules/description' ); ?>
<?php tribe_get_template_part( 'community/modules/datepickers' ); ?>
<?php tribe_get_template_part( 'community/modules/image' ); ?>
<?php tribe_get_template_part( 'community/modules/taxonomy', null, array( 'taxonomy' => Tribe__Events__Main::TAXONOMY ) ); ?>
<?php tribe_get_template_part( 'community/modules/taxonomy', null, array( 'taxonomy' => 'post_tag' ) ); ?>
<?php tribe_get_template_part( 'community/modules/venue' ); ?>
<?php tribe_get_template_part( 'community/modules/organizer' ); ?>
<?php tribe_get_template_part( 'community/modules/website' ); ?>
<?php tribe_get_template_part( 'community/modules/custom' ); ?>
<!-- This is the HTML to display our upload field. ToDo: style it nicely :-) -->
<div class="tribe-section">
<div class="tribe-section-header">
<h3>Event Flyer</h3>
</div>
<div class="tribe-section-content">
<table class="tribe-section-content">
<colgroup>
<col class="tribe-colgroup tribe-colgroup-label">
<col class="tribe-colgroup tribe-colgroup-field">
</colgroup>
<tbody><tr class="tribe-section-content-row">
<td class="tribe-section-content-label">
<label for="fish-flyer">Flyer for Event (in .pdf format)</label></td>
<td class="tribe-section-content-field">
<input type="file" name="fish-flyer" class="event-flyer">
</td>
</tr>
</tbody>
</table>
</div>
</div>
<?php tribe_get_template_part( 'community/modules/cost' ); ?>
<?php tribe_get_template_part( 'community/modules/spam-control' ); ?>
<?php tribe_get_template_part( 'community/modules/submit' ); ?>
</form>
<?php do_action( 'tribe_events_community_form_after_template', $tribe_event_id ); ?>
<?php
//PHP to handle the file upload and save it as a post meta field (on the event)
//save the custom flyer (pdf) field when an event is created/updated
function fish_save_flyer_meta( $post_id ) {
if ( !is_admin() && $_FILES['fish-flyer']['name'] ) {//only run this snippet on the front-end - and only if a file is being uploaded
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$uploadedfile = $_FILES['fish-flyer'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile && ! isset( $movefile['error'] ) ) {
update_post_meta( intval( $post_id ), 'event-flyer', $movefile['url'] );
} else {
echo $movefile['error'];
echo '<br>Please navigate back and try again.';
die();
}
}
}
add_action( 'tribe_events_update_meta', 'fish_save_flyer_meta', 10, 1 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment