Select Venue from dropdown list and reuse stored address information in meta_box
-
31-10-2019 - |
Question
UPDATED CODE 12/12/2011
Here is the code in functions.php. I put the code for output in my events page. For now there has been on output.
<?php
// Custom Meta Data for Events
$meta_box['event'] = array(
'id' => 'event-meta-details',
'title' => 'Event Information',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Start Date',
'desc' => '(Enter yyyy/mm/dd)',
'id' => 'start_date',
'type' => 'text',
'default' => ''
),
array(
'name' => 'Venue',
'desc' => 'Venue of Event',
'id' => $prefix . 'event_venue',
'type' => 'radio',
'options' => array(
array('name' => 'Spruce Gallery', 'value' => 'Spruce Gallery'),
array('name' => 'Pine Gallery', 'value' => 'Pine Gallery'),
array('name' => 'Oak Gallery', 'value' => 'Oak Gallery')
)
)
)
);
///////////////
function get_related_event_data( $venue, $field = 'all' ) {
$event_data = array(
'Spruce Gallery' => array(
'address' => 'Test Address 1',
'phone_no' => '344-333-2333',
'url' => 'http://www.url1.com'
),
'Pine Gallery' => array(
'address' => 'Test Address 2',
'phone_no' => '444-333-3333',
'url' => 'http://www.url2.com'
),
'Oak Gallery' => array(
'address' => 'Test Address 3',
'phone_no' => '555-666-6666',
'url' => 'http://www.url3.com'
),
);
if( !isset( $event_data[$venue] ) )
return;
// Optionally return all fields(note: you can't echo arrays, so be sure to loop over the data if pulling all fields
if( 'all' == $field )
return $event_data[$venue];
if( !isset( $event_data[$venue][$field] ) )
return;
return $event_data[$venue][$field];
}
///////////////
Here is the code I put on the page. I put in a test echo to make sure that the $venue
was getting information and it is, but the address does not display.
<?php
$venue = get_post_meta( $post->ID, 'event_venue', true );
echo $venue;
echo get_related_event_data( $venue, 'address' ); ?>
Old Post.
Here is the code that fills in my meta boxes.
// Custom Meta Data for Events
$meta_box['event'] = array(
'id' => 'event-meta-details',
'title' => 'Event Information',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Start Date',
'desc' => '(Enter yyyy/mm/dd)',
'id' => 'start_date',
'type' => 'text',
'default' => ''
),
array(
'name' => 'Venue',
'desc' => 'Venue of Event',
'id' => $prefix . 'event_venue',
'type' => 'radio',
'options' => array(
array('name' => 'Spruce Gallery', 'value' => 'Spruce Gallery', 'venue_url' => 'http://www.sprucegallery.com/'),
array('name' => 'Pine Gallery', 'value' => 'Pine Gallery'),
array('name' => 'Oak Gallery', 'value' => 'Oak Gallery')
)
)
)
);
Here is the code for saving the information.
// Add meta boxes to admin panel only needs to be added once
add_action('admin_menu', 'plib_add_box');
//Add meta boxes to post types
function plib_add_box() {
global $meta_box;
foreach($meta_box as $post_type => $value) {
add_meta_box($value['id'], $value['title'], 'plib_format_box', $post_type, $value['context'], $value['priority']);
}
}
//Format meta boxes
function plib_format_box() {
global $meta_box, $post;
// Use once for verification
echo '<input type="hidden" name="plib_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($meta_box[$post->post_type]['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>'.
'<th style="width:20%"><label for="'. $field['id'] .'">'. $field['name']. '</label></th>'.
'<td>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="'. $field['id']. '" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['default']) . '" size="30" style="width:30%" />'. ' - '. $field['desc'];
break;
case 'textarea':
echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . '</textarea>'. '<br />'. $field['desc'];
break;
case 'select':
echo '<select name="'. $field['id'] . '" id="'. $field['id'] . '">';
foreach ($field['options'] as $option) {
echo '<option '. ( $meta == $option ? ' selected="selected"' : '' ) . '>'. $option . '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ($field['options'] as $option) {
echo '<input type="radio" name="' . $field['id'] . '" value="' . $option['value'] . '"' . ( $meta == $option['value'] ? ' checked="checked"' : '' ) . ' />' . $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '"' . ( $meta ? ' checked="checked"' : '' ) . ' />';
break;
}
echo '<td>'.'</tr>';
}
echo '</table>';
}
// Save data from meta box
function plib_save_data($post_id) {
global $meta_box, $post;
//Verify nonce
if (!wp_verify_nonce($_POST['plib_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
//Check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
//Check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($meta_box[$post->post_type]['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
add_action('save_post', 'plib_save_data');
`I want to save and reuse multiple data points about multiple venues in an event CPT with meta boxes. I have the CPT and meta boxes that can save the event venue that I enter as well as times and dates. The goal is to have a drop down list of venues. When one of the venues is selected then the information for that venue - Address, URL, Phone #, etc. would be selected as well.
I planned to write out the information as an array but am not sure how to do that. Each time I try it doesn't work.
What is the best way to store multiple points of information without having to type them in each time? Worst case I will make a meta_key
for each bit of information and enter it by hand, but that seems like pointless data entry. There must be a better way.
Thank you in advance.
No correct solution