My Calendar has a custom field API to create additional fields for use in your events. My Calendar Pro includes a tool for creating and managing custom fields.
Custom Fields API demo file on Github
Create Input Field
/**
* Add custom fields that are sortable in My Calendar Pro.
*
* @param array $fields Array of fields available in Pro.
* @param boolean $has_data If true, this is an event being edited or corrected.
* @param object $event The event object saved.
* @param string $context 'public' or 'admin', depending on whether this is being rendered in the Pro submissions form or WP Admin.
*
* @since My Calendar Pro 2.0.0
*
* @return array of fields
*/
function mcs_event_email( $fields, $has_data, $event, $context ) {
if ( $has_data ) {
$post_id = $event->event_post;
/* Any custom fields are saved as custom post meta */
$email = get_post_meta( $post_id, '_mc_event_email', true );
} else {
$email = '';
}
$form = "<p><label for='event_email'>" . __( 'Contact Email', 'textdomain' ) . "</label> <input type='email' name='event_email' id='event_email' value='" . esc_attr( $email ) . "' /></p>";
$fields['event_email'] = $form;
return $fields;
}
add_filter( 'mc_custom_fields', 'mcs_event_email', 10, 5 );
Save Custom Field into Post Meta
/**
* Save custom fields into post meta.
*
* @param int $post_id ID of the post where event meta is saved.
* @param array $post $_POST array
* @param array $data Checked array of My Calendar data after processing.
* @param integer event_id ID of event in my_calendar custom table.
*
**/
add_action( 'mc_update_event_post', 'my_event_email_save', 10, 4 );
function my_event_email_save( $post_id, $post, $data, $event_id ) {
if ( is_email( $post['event_email'] ) ) {
$email = sanitize_text_field( $post['event_email'] );
update_post_meta( $post_id, '_mc_event_email', $email );
}
}
Add custom field into Template Tags
/**
* Add custom field into template tags array.
*
* @param array $details Array of template tags as $tag => $value
* @param object $event Event object as fetched from database.
*
* @return array $details
**/
add_filter( 'mc_filter_shortcodes', 'my_event_email_tag', 10, 2 );
function my_event_email_tag( $details, $event ) {
$post_id = $event->event_post;
/* Will be used as {contact_email} in templates. */
$details['contact_email'] = get_post_meta( $post_id, '_mc_event_email', true );
return $details;
}