wordpresssavemeta-boxes

Saving meta box with multiple fields


So, after looking for hours for just the simplest solution for saving my meta boxes, and finding nothing that could help me... I'm trying to save these metaboxes (I put the first one in the example below, the others are pretty much duplicates), and the saving method I'm using is doing nothing at all. Can you tell me what I'm doing wrong here? The saving method is from another plug-in a colleague of mine made, but I can't seem to get it to work...

add_action('admin_init','page_spec_meta');
function page_spec_meta()
{
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;     $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
    if ($template_file == 'index.php') 
    {
    add_meta_box("career-meta", "Career", "career_meta", "page", "normal", "high");
    add_meta_box("business-meta", "Business", "business_meta", "page", "normal", "high");
    add_meta_box("fabulous-meta", "Fabulous", "fabulous_meta", "page", "normal", "high");
    add_meta_box("network-meta", "Network", "network_meta", "page", "normal", "high");
    add_meta_box("future-meta", "Future", "future_meta", "page", "normal", "high");
    }
}

// --- METABOX: CAREER ... CONTENTS --- //
function career_meta(){
    global $post;
    $career = get_post_meta( $post->ID, 'career', true );
?>
    <label for="career subtitle">Subtitle</label><input type="text" class="widefat" id="career-subtitle" name="career subtitle" value="<?php echo $career; ?>" />
    <label for="career text 1">Left Column</label><textarea class="widefat" id="career-text-1" name="career text 1" value="<?php echo $career; ?>"></textarea>
    <label for="career text 2">Right Column</label><textarea class="widefat" id="career-text-2" name="career text 2" value="<?php echo $career; ?>"></textarea>
<?php }

// --- METABOX: CAREER ... SAVE --- //
add_action('save_post', 'save_career');
function save_career(){
    global $post;
    update_post_meta($post->ID, "career", $_POST["career"]);
}

add_action('save_post','function_save_var');
function function_save_var()
{
  if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
    return $post_id;

}

What works: Metaboxes display on the page I specified, and are editable What doesn't work: Saving...


Solution

  • The name attribute of the input (or textarea) field needs to be used when accessing the $_POST array. Your names are "career subtitle", "career text 1", and "career text 2", so you need to use $_POST["career subtitle"], $_POST["career text 1"], or $_POST["career text 2"] to access the values passed.

    I'm not certain whether you'll have problems with the browser escaping the spaces and PHP needing to unescape them to find them in $_POST - I'd probably replace the spaces in the names with underscores just in case, for example

    <label for="career_subtitle">Subtitle</label><input type="text" class="widefat" id="career-subtitle" name="career_subtitle" value="<?php echo $career; ?>" />
    

    then in save_career

    update_post_meta($post->ID, "career", $_POST["career_subtitle"]);
    

    Or, if you need the three values stored separately:

    update_post_meta($post->ID, "career_subtitle", $_POST["career_subtitle"]);
    

    with a corresponding changes in career_meta when you retrieve the value.