Skip to content

Instantly share code, notes, and snippets.

@joshuadavidnelson
Created September 25, 2014 16:49
Show Gist options
  • Save joshuadavidnelson/ab8e39fe5cc70d1f5b2b to your computer and use it in GitHub Desktop.
Save joshuadavidnelson/ab8e39fe5cc70d1f5b2b to your computer and use it in GitHub Desktop.
Option to remove a post from a recurring event series in BE-Events-Calendar
<?php
/**
* Add the ability to remove a single event from a series with a checkbox (using CMB for metaboxes)
*
* Uses Bill Erickon's BE-Events-Calendar, however this example uses Custom Metaboxes & Fields (CMB) for the events meta options
*
* @author Joshua David Nelson
*/
// Remove event from series
add_action( 'updated_post_meta', 'jdn_remove_recurring_event', 10, 4 );
function remove_recurring_event( $meta_id, $post_id, $meta_key='', $meta_value='' ) {
if( 'events' !== get_post_type( $post_id ) )
return;
// Change this condition based on the meta field type of your events calendar plugin
$recurring_event = get_post_meta( $post_id, 'recurring_event', true );
$remove_recurring_event = get_post_meta( $post_id, 'remove_recurring_event', true );
if( $recurring_event && $remove_recurring_event == 'on' ) {
// Remove the associated recurring event, and remove the meta option to remove the recurring event
delete_post_meta( $post_id, 'recurring_event' );
delete_post_meta( $post_id, 'remove_recurring_event' );
}
}
<?php
/**
* This creates a event metaboxes with CMB, as well as a checkbox to remove from a series,
* only if the event is part of series (recurring event)
*
* @author Joshua David Nelson
*/
/**
* Override BE-Events-Calendar metaboxes
*
* @link https://github.com/billerickson/BE-Events-Calendar/blob/master/be-events-calendar.php#L61
*/
add_filter( 'be_events_manager_metabox_override', 'jdn_override_events_metaboxes' );
function jdn_override_events_metaboxes( $metabox ) {
return true;
}
/**
* Initialize the metabox framework
*
* @link https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress/wiki/Basic-Usage#initialize-metaboxes
*/
add_action( 'init', 'cmb_initialize_meta_boxes', 9999 );
function cmb_initialize_meta_boxes() {
if ( !class_exists( 'cmb_Meta_Box' ) ) {
require_once( 'lib/metabox/init.php' );
}
}
// Add Custom Metaboxes
add_filter( 'cmb_meta_boxes', 'jdn_event_metaboxes' );
function jdn_event_metaboxes( $meta_boxes ) {
$prefix = 'event_';
$fields = array(
array(
'name' => 'Event Host',
'id' => $prefix . 'host',
'desc' => 'Optional',
'type' => 'text_medium',
),
array(
'name' => 'Host Email Address',
'id' => $prefix . 'host_email',
'desc' => 'Optional',
'type' => 'text_email',
),
array(
'name' => 'Host Phone Number',
'id' => $prefix . 'host_phone',
'desc' => 'Optional',
'type' => 'text_medium',
),
array(
'name' => 'All Day Event',
'id' => $prefix . 'all_day',
'type' => 'checkbox',
),
array(
'name' => 'Event Start Date',
'id' => $prefix . 'start',
'type' => 'text_datetime_timestamp',
),
array(
'name' => 'Event End Date',
'id' => $prefix . 'end',
'type' => 'text_datetime_timestamp',
),
array(
'name' => 'Event Location',
'id' => $prefix . 'location',
'type' => 'address',
),
);
// Grab post id
if( isset( $_GET['post'] ) ) {
$post_id = $_GET['post'];
} elseif( isset( $_POST['post_ID'] ) ) {
$post_id = $_POST['post_ID'];
}
// Add option to remove from post series if the post is a recurring event, if there is a post id available
if( isset( $post_id ) ) {
$recurring = get_post_meta( $post_id, 'recurring_event', true );
if( !empty( $recurring ) && get_post_type( $post_id ) != 'recurring-events' ) {
$fields[] = array(
'name' => 'Remove from Post Series',
'id' => 'remove_recurring_event',
'type' => 'checkbox',
'desc' => 'Check to remove from recurring event series. Warning: Cannot be undone.',
);
}
} else {
$post_id = null;
}
$event_metabox = array(
'id' => 'event_details',
'title' => 'Event Details',
'pages' => array( 'events', 'recurring-events' ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
'fields' => apply_filters( 'jdn_event_meta_fields', $fields, $post_id ), // allowing you to filter metabox fields
);
// Use this to override the metabox entirely and create your own
$override = apply_filters( 'jdn_event_metabox_override', false );
if ( false === $override ) $meta_boxes[ $event_metabox['id'] ] = $event_metabox;
return $meta_boxes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment