VANTAGE: Custom Fields in Admin - some progress & a question
I've been following various forum discussions for a week or two about how to see VANTAGE custom fields in the admin panel. This is really quite a problem for me (and for lots of others it seems), and it never entered my head that this functionality wouldn't be there.
Anyway, it's not currently there, so I've had to make it happen. This isn't pretty and it isn't finished, but here's what I've added to my child theme's functions.php to get the custom fields (that were built using Vantage Custom Forms) showing in the admin panel.
So far, I've successfully got text fields and select fields working nicely between front and admin - updating either side correctly, and displaying correctly.
My one remaining problem is that I can't get my multiple checkboxes to save properly. I know I'm being dim, but I just can't see what I'm doing wrong. I'm hoping that in return for sharing this much, someone can help me out on how to save my multiple checkboxes. My warning is that everything is working great apart from the checkbox saving (and I need to do some tidying up there too...). My checkbox field is called AgeGroup, and it's intended that multiple options can be selected.
Here's my current code:
add_action("admin_init", "listings_addnl_meta");
function listings_addnl_meta(){
add_meta_box("TestGroup", "Listings Additional Info", "TestGroup", "listing", "normal", "high");
}
function TestGroup(){
global $post;
$custom = get_post_custom($post->ID);
$agegroup = $custom["app_agegroup"][0];
$needtobook = $custom["app_needtobook"][0];
$price = $custom["app_price"][0];
$contactemailaddress = $custom["app_contactemailaddress"][0];
$hours = $custom["app_hours"][0];
?>
<label>Age Group:</label><br>
<?php
$length = count($custom["app_agegroup"]);
echo '<input type="checkbox" name="app_agegroup[]" value="Under 5"';
for ($i = 0; $i < $length; $i++) {
if($custom["app_agegroup"][$i] == 'Under 5'){echo 'checked';}else{echo '';};
}
echo' /> Under 5<br>';
echo '<input type="checkbox" name="app_agegroup[]" value="5 - 10 Years Old"';
for ($i = 0; $i < $length; $i++) {
if($custom["app_agegroup"][$i] == '5 - 10 Years Old'){echo 'checked';}else{echo '';};
}
echo' /> 5 - 10 Years Old<br>';
echo '<input type="checkbox" name="app_agegroup[]" value="10 - 18 Years Old"';
for ($i = 0; $i < $length; $i++) {
if($custom["app_agegroup"][$i] == '10 - 18 Years Old'){echo 'checked';}else{echo '';};
}
echo' /> 10 - 18 Years Old<br>';
echo '<input type="checkbox" name="app_agegroup[]" value="Adult"';
for ($i = 0; $i < $length; $i++) {
if($custom["app_agegroup"][$i] == 'Adult'){echo 'checked';}else{echo '';};
}
echo' /> Adult<br>';
?>
<br><br>
<label>Price:</label>
<input id="app_price" name="app_price" type="text" value="<?php echo $price; ?>" /><br><br>
<label>Contact Email Address:</label>
<input id="app_contactemailaddress" name="app_contactemailaddress" type="text" value="<?php echo $contactemailaddress; ?>" /><br><br>
<label>Hours:</label>
<input id="app_hours" name="app_hours" type="text" value="<?php echo $hours; ?>" /><br><br>
<label>Need to Book:</label>
<select name="app_needtobook" id="app_needtobook">
<option></option>
<option value="Yes" <?php selected( $needtobookselected, 'Yes' ); ?>>Yes</option>
<option value="No" <?php selected( $needtobookselected, 'No' ); ?>>No</option>
</select>
<?php
}
add_action('save_post', 'save_meta');
function save_meta(){
global $post;
update_post_meta($post->ID, app_agegroup, $_POST["app_agegroup"]);
for ($i = 0; $i < 5; $i++) {
if(isset($_POST["app_agegroup"][$i])) {
update_post_meta($post->ID, 'app_agegroup[$i]', $_POST['app_agegroup'][$i]);
}
}
update_post_meta($post->ID, app_price, $_POST["app_price"]);
update_post_meta($post->ID, app_contactemailaddress, $_POST["app_contactemailaddress"]);
update_post_meta($post->ID, app_hours, $_POST["app_hours"]);
update_post_meta($post->ID, app_needtobook, $_POST["app_needtobook"]);
}