Venue meta data and metaboxes
Since 1.4 the venue table has been converted into a metadata table – that is, it follows the same structure as the post meta, user meta etc tables. Each row has its own meta ID, a venue (term) ID, a meta key and a meta value.
Naming conventions
To ‘future proof’ your metadata, it’s advised that you precede your keys with an underscore. For instance, use the meta key, _opening_time
rather than opening_time
.
Adding metabox onto the venue page
Adding a metabox to the venue page is (almost) identical to adding a metabox to a post type. To add a metabox you can use the usual add_meta_boxes
action or the add_meta_boxes_event_page_venues
action, following the WordPress standard of add_meta_boxes_{$screen_id}
. Inside that hooked callback you add the metabox by calling add_meta_box
. Where the $post_type
(or screen ID) is set to event_page_venues
For instance
add_action('add_meta_boxes','my_add_metabox');
function my_add_metabox(){
add_meta_box('my_id','My Title', 'my_metabox_callback', 'event_page_venues', 'side', 'high');
}
function my_metabox_callback( $venue ){
//Metabox's innards:
$time = eo_get_venue_meta($venue->term_id, '_opening_times',true);
//Remember to use nonces!
wp_nonce_field('my_venue_meta_save', 'my_plugin_nonce_field' );
?>
<label> Opening times:</label>
<input type="text" name="my_opening_time" value="<?php echo esc_attr($time);?>" >
<?php
}
Saving and retrieving meta data.
Rather than hooking onto save_post
its preferable to hook onto eventorganiser_save_venue
. You can then proceed ‘as normal’ (as you would with save_post
): checking nonces, permissions, and collecting your metaboxes data from $_POST
:
add_action ('eventorganiser_save_venue','my_save_venue_meta');
function my_save_venue_meta( $venue_id ){
//If our nonce isn't present just silently abort.
if( !isset( $_POST['my_plugin_nonce_field'] ) )
return;
//Check permissions
$tax = get_taxonomy( 'event-venue');
if ( !current_user_can( $tax->cap->edit_terms ) )
return;
//Check nonce
check_admin_referer( 'my_venue_meta_save', 'my_plugin_nonce_field' );
//Retrieve meta value(s)
$value = $_POST['my_opening_time'];
//Update venue meta
eo_update_venue_meta($venue_id, '_opening_times', $value);
return;
}
The following functions are almost identical to get_post_meta
, add_post_meta
, update_post_meta
, delete_post_meta
. In all instances the arguments are same – except rather than a post ID you pass the venue (term) ID.
eo_get_venue_meta($venue_id, $key, $single)
eo_add_venue_meta($venue_id, $key, $value, $unique )
eo_update_venue_meta($venue_id, $key, $value, $prev_value)
eo_delete_venue_meta($venue_id, $key, $value, $delete_all )
Reserved meta keys:
The following keys are used by the plug-in, so in order not to cause unwanted behaviour please avoid them: _address
, _postcode
,_country
,_lat
,_lng
,_description