I’m possibly biting off more than I can choose. I haven’t deal with creating custom fields and panels to this extent before. I’m working off of this tutorial:
http://wefunction.com/2008/10/tutorial-creating-custom-write-panels-in-wordpress/
I have created custom fields for a few post types. The fields display properly for each post type. However the custom fields are only saving properly within one of the post types, “Project”. Based on the tutorial (and my probably questionable code below) can anyone point me in the right direction on how to properly save custom field data. Any tutorials that offer more details would be great.
<?php
//Meta boxes with upload fields
if ( is_admin() ) {
function add_post_enctype() {
echo "<script type='text/javascript'>
jQuery(document).ready(function(){
jQuery('#post').attr('enctype','multipart/form-data');
});
</script>";
}
add_action('admin_head', 'add_post_enctype');
}
//Project Meta Box Variable Array
$project_meta_boxes =
array(
"start" => array(
"name" => "start",
"type" => "start"
),
"image" => array(
"name" => "mainimage",
"std" => "",
"type" => "image",
"title" => "Main Image"
),
//END Project Meta Box Variable Array
//Product Meta Box Variable Array
$product_meta_boxes =
array(
"start" => array(
"name" => "start",
"type" => "start"
),
"google_checkout" => array(
"name" => "google_checkout",
"std" => "",
"type" => "text",
"title" => "Google Checkout Link",
"description" => "Purchase Link"
),
"end" => array(
"name" => "end",
"type" => "end"
),
);
//END Product Meta Box Variable Array
//Feature Meta Box Variable Array
$feature_meta_boxes =
array(
"start" => array(
"name" => "start",
"type" => "start"
),
"url" => array(
"name" => "url",
"std" => "",
"type" => "text",
"title" => "URL for featured item",
"description" => "URL"
),
"feature_post_id" => array(
"name" => "feature_post_id",
"std" => "",
"type" => "text",
"title" => "Post ID for featured item",
"description" => "Post ID"
),
"tagline" => array(
"name" => "tagline",
"std" => "",
"type" => "text",
"title" => "Tagline",
"description" => "Tagline to your post"
),
"featureimage" => array(
"name" => "bannerimage",
"std" => "",
"type" => "image",
"title" => "Feature Image"
),
"end" => array(
"name" => "end",
"type" => "end"
),
);
//END Feature Meta Box Variable Array
//Product Meta Boxes =========================
//============================================
function product_meta_boxes() {
global $post, $product_meta_boxes;
foreach($product_meta_boxes as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'', true);
if($meta_box_value == "")
$meta_box_value = $meta_box['std'];
echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
if($meta_box['type'] == "start") {
echo "<div class='optionsbox'><style type='text/css'>.optionsbox {
display:block;
width:auto;
float:none;
overflow: hidden;
}
.optionsbox input, .optionsbox textarea {
outline:none;
padding:5px;
color:#999;
}
.optionsbox input:focus, .optionsbox textarea:focus {
border-color:#999;
color:#666;
}
.optionsbox p {
margin-bottom:20px;
}
.optionsbox label {
width:140px;
display:block;
float:left;
margin-top:3px;
}
.optionsbox small {
padding-left:140px;
padding-top:3px;
color:#999;
}
</style>";
} else if($meta_box['type'] == "end") {
echo '</div>';
} else if($meta_box['type'] == "image") {
echo $meta_box['before'];
echo '<div style="background:#f4f4f4;padding:10px;height:120px;margin:0 0 20px 0;display:block">';
if($meta_box_value) {
echo '<img style="float:right" src="'.get_bloginfo('template_directory').'/scripts/timthumb.php?src='.$meta_box_value.'&w=120&h=120" alt="" />';
}
echo'<p><label for="'.$meta_box['name'].'_upload">'.$meta_box['title'].'</label>';
echo'<input type="file" name="'.$meta_box['name'].'_upload" size="55" /><br />';
echo'<small>Upload image here</small></p>';
echo'<p><label> </label>';
echo'<input type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';
echo'<small>or add a URL to the image here</small></p>';
echo '</div>';
} else if($meta_box['type'] == "text") {
echo $meta_box['before'];
echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
echo'<input style="color:#666;" type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';
echo'<small style="padding-left:140px;padding-top:3px;">'.$meta_box['description'].'</small></p>';
} else if($meta_box['type'] == "checkbox") {
echo $meta_box['before'];
echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
if($meta_box_value) {
$checked = "checked=\\"checked\\"";
} else {
$checked = "";
}
echo '<input style="display:block;float:left;width:20px;margin:5px 0 0 0;" '.$checked.' type="checkbox" name="'.$meta_box['name'].'" /><br/>';
echo'<small style="clear:both;padding-left:140px;padding-top:3px;display:block;">'.$meta_box['description'].'</small></p>';
} else if($meta_box['type'] == "textarea") {
echo $meta_box['before'];
echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
echo'<textarea style="color:#666;" name="'.$meta_box['name'].'" cols="50" rows="4">'.stripslashes($meta_box_value).'</textarea><br />';
echo'<small style="padding-left:140px;padding-top:3px;">'.$meta_box['description'].'</small></p>';
}
}
}
//Project Meta Boxes =========================
//============================================
function project_meta_boxes() {
global $post, $project_meta_boxes;
foreach($project_meta_boxes as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'', true);
if($meta_box_value == "")
$meta_box_value = $meta_box['std'];
echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
if($meta_box['type'] == "start") {
echo "<div class='optionsbox'><style type='text/css'>.optionsbox {
display:block;
width:auto;
float:none;
overflow: hidden;
}
.optionsbox input, .optionsbox textarea {
outline:none;
padding:5px;
color:#999;
}
.optionsbox input:focus, .optionsbox textarea:focus {
border-color:#999;
color:#666;
}
.optionsbox p {
margin-bottom:20px;
}
.optionsbox label {
width:140px;
display:block;
float:left;
margin-top:3px;
}
.optionsbox small {
padding-left:140px;
padding-top:3px;
color:#999;
}
</style>";
} else if($meta_box['type'] == "end") {
echo '</div>';
} else if($meta_box['type'] == "image") {
echo $meta_box['before'];
echo '<div style="background:#f4f4f4;padding:10px;height:120px;margin:0 0 20px 0;display:block">';
if($meta_box_value) {
echo '<img style="float:right" src="'.get_bloginfo('template_directory').'/scripts/timthumb.php?src='.$meta_box_value.'&w=120&h=120" alt="" />';
}
echo'<p><label for="'.$meta_box['name'].'_upload">'.$meta_box['title'].'</label>';
echo'<input type="file" name="'.$meta_box['name'].'_upload" size="55" /><br />';
echo'<small>Upload image here</small></p>';
echo'<p><label> </label>';
echo'<input type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';
echo'<small>or add a URL to the image here</small></p>';
echo '</div>';
} else if($meta_box['type'] == "text") {
echo $meta_box['before'];
echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
echo'<input style="color:#666;" type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';
echo'<small style="padding-left:140px;padding-top:3px;">'.$meta_box['description'].'</small></p>';
} else if($meta_box['type'] == "checkbox") {
echo $meta_box['before'];
echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
if($meta_box_value) {
$checked = "checked=\\"checked\\"";
} else {
$checked = "";
}
echo '<input style="display:block;float:left;width:20px;margin:5px 0 0 0;" '.$checked.' type="checkbox" name="'.$meta_box['name'].'" /><br/>';
echo'<small style="clear:both;padding-left:140px;padding-top:3px;display:block;">'.$meta_box['description'].'</small></p>';
} else if($meta_box['type'] == "textarea") {
echo $meta_box['before'];
echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
echo'<textarea style="color:#666;" name="'.$meta_box['name'].'" cols="50" rows="4">'.stripslashes($meta_box_value).'</textarea><br />';
echo'<small style="padding-left:140px;padding-top:3px;">'.$meta_box['description'].'</small></p>';
}
}
}
//Feature Meta Boxes =========================
//============================================
function feature_meta_boxes() {
global $post, $feature_meta_boxes;
foreach($feature_meta_boxes as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'', true);
if($meta_box_value == "")
$meta_box_value = $meta_box['std'];
echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
if($meta_box['type'] == "start") {
echo "<div class='optionsbox'><style type='text/css'>.optionsbox {
display:block;
width:auto;
float:none;
overflow: hidden;
}
.optionsbox input, .optionsbox textarea {
outline:none;
padding:5px;
color:#999;
}
.optionsbox input:focus, .optionsbox textarea:focus {
border-color:#999;
color:#666;
}
.optionsbox p {
margin-bottom:20px;
}
.optionsbox label {
width:140px;
display:block;
float:left;
margin-top:3px;
}
.optionsbox small {
padding-left:140px;
padding-top:3px;
color:#999;
}
</style>";
} else if($meta_box['type'] == "end") {
echo '</div>';
} else if($meta_box['type'] == "image") {
echo $meta_box['before'];
echo '<div style="background:#f4f4f4;padding:10px;height:120px;margin:0 0 20px 0;display:block">';
if($meta_box_value) {
echo '<img style="float:right" src="'.get_bloginfo('template_directory').'/scripts/timthumb.php?src='.$meta_box_value.'&w=120&h=120" alt="" />';
}
echo'<p><label for="'.$meta_box['name'].'_upload">'.$meta_box['title'].'</label>';
echo'<input type="file" name="'.$meta_box['name'].'_upload" size="55" /><br />';
echo'<small>Upload image here</small></p>';
echo'<p><label> </label>';
echo'<input type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';
echo'<small>or add a URL to the image here</small></p>';
echo '</div>';
} else if($meta_box['type'] == "text") {
echo $meta_box['before'];
echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
echo'<input style="color:#666;" type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';
echo'<small style="padding-left:140px;padding-top:3px;">'.$meta_box['description'].'</small></p>';
} else if($meta_box['type'] == "checkbox") {
echo $meta_box['before'];
echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
if($meta_box_value) {
$checked = "checked=\\"checked\\"";
} else {
$checked = "";
}
echo '<input style="display:block;float:left;width:20px;margin:5px 0 0 0;" '.$checked.' type="checkbox" name="'.$meta_box['name'].'" /><br/>';
echo'<small style="clear:both;padding-left:140px;padding-top:3px;display:block;">'.$meta_box['description'].'</small></p>';
} else if($meta_box['type'] == "textarea") {
echo $meta_box['before'];
echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
echo'<textarea style="color:#666;" name="'.$meta_box['name'].'" cols="50" rows="4">'.stripslashes($meta_box_value).'</textarea><br />';
echo'<small style="padding-left:140px;padding-top:3px;">'.$meta_box['description'].'</small></p>';
}
}
}
//Create Meta Boxes
function create_meta_box() {
global $theme_name;
// Create Meta boxes for Project Post Type
if ( function_exists('add_meta_box') ) {
foreach ( array('projects') as $type ) {
add_meta_box( 'project-meta-boxes', 'Project Options', 'project_meta_boxes', $type, 'normal', 'high' );
}
}
// Create Meta boxes for Product Post Type
if ( function_exists('add_meta_box') ) {
foreach ( array('products') as $type ) {
add_meta_box( 'product-meta-boxes', 'Product Options', 'product_meta_boxes', $type, 'normal', 'high' );
}
}
// Create Meta boxes for Feature Post Type
if ( function_exists('add_meta_box') ) {
foreach ( array('features') as $type ) {
add_meta_box( 'feature-meta-boxes', 'Feature Options', 'feature_meta_boxes', $type, 'normal', 'high' );
}
}
}
function save_postdata( $post_id ) {
global $post, $project_meta_boxes, $product_meta_boxes, $feature_meta_boxes;
$post_id = wp_is_post_revision($post_id);
//Project
foreach($project_meta_boxes as $meta_box) {
if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ))
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;
}
$imageuploadlocation = "";
$metaboxname = "";
$metaboxname_upload = "";
if($meta_box['type'] == 'image') {
$metaboxname = $meta_box['name'];
$metaboxname_upload = $metaboxname.'_upload';
if($_FILES[$metaboxname_upload]['name'] != "") {
$overrides = array( 'test_form' => false);
$imagefile=wp_handle_upload($_FILES[$metaboxname_upload], $overrides);
$imageuploadlocation = $imagefile['url'];
delete_post_meta($post_id, $metaboxname, get_post_meta($post_id, $metaboxname, true));
add_post_meta($post_id, $metaboxname, $imageuploadlocation, true);
} else {
$imageuploadlocation = get_post_meta($post_id, $metaboxname, true);
delete_post_meta($post_id, $metaboxname, get_post_meta($post_id, $metaboxname, true));
add_post_meta($post_id, $metaboxname, $_POST[$metaboxname], true);
}
} else {
$data = $_POST[$meta_box['name'].''];
if(get_post_meta($post_id, $meta_box['name'].'') == "")
add_post_meta($post_id, $meta_box['name'].'', $data, true);
elseif($data != get_post_meta($post_id, $meta_box['name'].'', true))
update_post_meta($post_id, $meta_box['name'].'', $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'].'', get_post_meta($post_id, $meta_box['name'].'', true));
}
}
//Product
foreach($product_meta_boxes as $meta_box) {
if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ))
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;
}
$imageuploadlocation = "";
$metaboxname = "";
$metaboxname_upload = "";
if($meta_box['type'] == 'image') {
$metaboxname = $meta_box['name'];
$metaboxname_upload = $metaboxname.'_upload';
if($_FILES[$metaboxname_upload]['name'] != "") {
$overrides = array( 'test_form' => false);
$imagefile=wp_handle_upload($_FILES[$metaboxname_upload], $overrides);
$imageuploadlocation = $imagefile['url'];
delete_post_meta($post_id, $metaboxname, get_post_meta($post_id, $metaboxname, true));
add_post_meta($post_id, $metaboxname, $imageuploadlocation, true);
} else {
$imageuploadlocation = get_post_meta($post_id, $metaboxname, true);
delete_post_meta($post_id, $metaboxname, get_post_meta($post_id, $metaboxname, true));
add_post_meta($post_id, $metaboxname, $_POST[$metaboxname], true);
}
} else {
$data = $_POST[$meta_box['name'].''];
if(get_post_meta($post_id, $meta_box['name'].'') == "")
add_post_meta($post_id, $meta_box['name'].'', $data, true);
elseif($data != get_post_meta($post_id, $meta_box['name'].'', true))
update_post_meta($post_id, $meta_box['name'].'', $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'].'', get_post_meta($post_id, $meta_box['name'].'', true));
}
}
//Feature
foreach($feature_meta_boxes as $meta_box) {
if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ))
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;
}
$imageuploadlocation = "";
$metaboxname = "";
$metaboxname_upload = "";
if($meta_box['type'] == 'image') {
$metaboxname = $meta_box['name'];
$metaboxname_upload = $metaboxname.'_upload';
if($_FILES[$metaboxname_upload]['name'] != "") {
$overrides = array( 'test_form' => false);
$imagefile=wp_handle_upload($_FILES[$metaboxname_upload], $overrides);
$imageuploadlocation = $imagefile['url'];
delete_post_meta($post_id, $metaboxname, get_post_meta($post_id, $metaboxname, true));
add_post_meta($post_id, $metaboxname, $imageuploadlocation, true);
} else {
$imageuploadlocation = get_post_meta($post_id, $metaboxname, true);
delete_post_meta($post_id, $metaboxname, get_post_meta($post_id, $metaboxname, true));
add_post_meta($post_id, $metaboxname, $_POST[$metaboxname], true);
}
} else {
$data = $_POST[$meta_box['name'].''];
if(get_post_meta($post_id, $meta_box['name'].'') == "")
add_post_meta($post_id, $meta_box['name'].'', $data, true);
elseif($data != get_post_meta($post_id, $meta_box['name'].'', true))
update_post_meta($post_id, $meta_box['name'].'', $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'].'', get_post_meta($post_id, $meta_box['name'].'', true));
}
}
}
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata', 12);
?>