1

I have created multiple post types and now want to create multiple custom fields for each post type. I am not entirely how to but more specifically I believe I am doing something wrong when trying to save the meta field data.

I've based my project form this tutorial: http://wefunction.com/2008/10/tutorial-creating-custom-write-panels-in-wordpress/

So far the custom fields save properly for one of the post types - Project post type. The meta data does not work for the other post types.

My code is below however it may easier to look at the tutorial and decipher how to set it up for multiple post types.

<?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>&nbsp;</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>&nbsp;</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>&nbsp;</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); 
?>
5
  • That's quite alot of code there, any chance you can trim it down to just the relevant part you're having an issue with?
    – t31os
    Commented Dec 15, 2011 at 19:35
  • Yeah, I need to. Sorry. I'm still learning some of the details and haven't gone through the code well enough to trim it down. I will try to do that.
    – rpeg
    Commented Dec 15, 2011 at 19:49
  • Close voting as too localized. See my answer, it's just an overseeing in some copy/paste (or what else the source was) code.
    – kaiser
    Commented Dec 15, 2011 at 21:52
  • Btw: @rpeg always look at the sources date. It's from 2008...
    – kaiser
    Commented Dec 15, 2011 at 21:54
  • I could barely find any documentation on this, yeah.
    – rpeg
    Commented Dec 15, 2011 at 22:08

2 Answers 2

1

I just jumped in deep on the last two WordPress projects that I've built getting immersed on both custom post types and custom fields, which are frequently implemented using metaboxes on the back end.

On this last project I went all out with the use of WP Alchemy metabox class for WordPress. It's not really a plugin, it's more of a PHP class that piggy-backs on WordPress, allowing you to create metabox fields for your pages, posts, and custom post types in the admin.

Only after I nearly completed this project did I come across Justin Taddock's straight forward approach to metaboxes. Albeit, the WP Alchemy class has some nice additional features that I made good use of, so I think I'd still recommend it above Justin's tutorial, but both are good reads to get you going in the right direction.

0

I guess you're new to php too...

Take a look at the loops inside the create_meta_box function and the following line:

foreach ( array('projects') as $type ) {

I guess that says it all...

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.