Created
July 25, 2017 11:48
-
-
Save panoslyrakis/3ba657a9c1ff1789caf4c57d2a8f6a5c to your computer and use it in GitHub Desktop.
Add PostalAddress metabox to events. Can be used in events schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: WPMUDEV - Events PostalAddress | |
Plugin URI: https://premium.wpmudev.org/ | |
Description: Add PostalAddress metabox to events | |
Author: Panos Lyrakis @ WPMUDEV | |
Author URI: https://premium.wpmudev.org/ | |
License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_Events_PostalAddress' ) ) { | |
class WPMUDEV_Events_PostalAddress { | |
private static $_instance = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_Events_PostalAddress(); | |
} | |
return self::$_instance; | |
} | |
public function __construct() { | |
add_action( 'add_meta_boxes_incsub_event', array( $this, 'add_address_meta_box' ), 10 ); | |
add_action( 'save_post', array( $this, 'save_address_meta' ), 10, 2 ); | |
} | |
public function add_address_meta_box( $post ){ | |
add_meta_box( | |
'wpmudev-PostalAddress_metabox', | |
__( 'Event Address' ), | |
array( $this, 'event_address_metabox' ), | |
$post->post_type, | |
'side', | |
'core' | |
); | |
} | |
public function event_address_metabox( $post ){ | |
$address_data = get_post_meta( $post->ID, 'incsub_event_address', true ); | |
$street_address = isset( $address_data[ 'streetAddress' ] ) ? $address_data[ 'streetAddress' ] : ''; | |
$address_locality = isset( $address_data[ 'addressLocality' ] ) ? $address_data[ 'addressLocality' ] : ''; | |
$address_region = isset( $address_data[ 'addressRegion' ] ) ? $address_data[ 'addressRegion' ] : ''; | |
$postal_code = isset( $address_data[ 'postalCode' ] ) ? $address_data[ 'postalCode' ] : ''; | |
$content = ''; | |
$content .= '<div class="eab_meta_box">'; | |
$content .= '<input type="hidden" name="incsub_event_address_meta" value="1" />'; | |
$content .= '<div class="misc-eab-section" >'; | |
$content .= '<div class="eab_meta_column_box top"><label for="incsub_event_address" id="incsub_event_address_label">'.__('Event address').'</label></div>'; | |
$content .= '<div class="misc-eab-section eab-address-section">'; | |
$content .= '<div class="misc-eab-subsection eab-address-subsection">'; | |
$content .= '<label>' . __('Street address') . '</label> '; | |
$content .= '<input type="text" name="incsub_event_address[streetAddress]" value="' . $street_address . '" />'; | |
$content .= '</div>'; | |
$content .= '<div class="misc-eab-subsection eab-address-subsection"> '; | |
$content .= '<label>' . __('Locality') . '</label>'; | |
$content .= '<input type="text" name="incsub_event_address[addressLocality]" value="' . $address_locality . '" />'; | |
$content .= '</div>'; | |
$content .= '<div class="misc-eab-subsection eab-address-subsection"> '; | |
$content .= '<label>' . __('Region') . '</label>'; | |
$content .= '<input type="text" name="incsub_event_address[addressRegion]" value="' . $address_region . '" />'; | |
$content .= '</div>'; | |
$content .= '<div class="misc-eab-subsection eab-address-subsection"> '; | |
$content .= '<label>' . __('ZIP Code') . '</label>'; | |
$content .= '<input type="text" name="incsub_event_address[postalCode]" value="' . $postal_code . '" />'; | |
$content .= '</div>'; | |
$content .= '</div>'; | |
$content .= '</div>'; | |
$content .= '</div>'; | |
echo $content; | |
} | |
public function save_address_meta( $post_id, $post ){ | |
if( !isset( $_POST['incsub_event_address'] ) || !current_user_can( 'edit_post', $post_id ) || $post->post_type != 'incsub_event' ){ | |
return; | |
} | |
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){ | |
return $post_id; | |
} | |
update_post_meta($post_id, 'incsub_event_address', array_map('strip_tags', $_POST['incsub_event_address']) ); | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
$GLOBALS['WPMUDEV_Events_PostalAddress'] = WPMUDEV_Events_PostalAddress::get_instance(); | |
}, 10 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately currently there is no filter to edit the schema of the event. In order to add the PostalAddress markup in the event you should edit file:
events-and-bookings/lib/class_eab_template.php
and change method get_event_details() to
`public static function get_event_details ($post) {
$content = '';
$data = Eab_Options::get_instance();
$event = ($post instanceof Eab_EventModel) ? $post : new Eab_EventModel($post);