Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following code. I am trying to enter data into the custom metabox and then save it. When I go to view it has entered a post, however the name column has nothing in it. Also when I hit save I get a post.php page with a blank page, is this a page I need to create in my theme and have it go back to the current page or something.

<?
/**
*   Custom post-type booktime
**/

add_action('init', 'booktime_register');

function booktime_register() {

    $labels = array(
        'name' => _x('booktime', 'post type general name'),
        'singular_name' => _x('booktime', 'post type singular name'),
        'add_new' => _x('add new', 'add new'),
        'add_new_item' => __('add new item'),
        'edit_item' => __('edit item'),
        'new_item' => __('new item'),
        'view_item' => __('view item'),
        'search_items' => __('search items'),
        'not_found' =>  __('nothing here'),
        'not_found_in_trash' => __('not found in trash'),
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','thumbnail','comments','custom-fields','revisions')
      );

    register_post_type( 'booktime' , $args );
}

?>

<?php

add_action("manage_posts_custom_column",  "booktime_custom_columns");
add_filter("manage_edit-booktime_columns", "booktime_edit_columns");

function booktime_custom_columns($column){
    global $post;

    switch ($column) {
        case "name":
            the_excerpt();
        break;
        case "address":
            $custom = get_post_custom();
            echo $custom["address"][0];
        break;
        case "phone":
            $custom = get_post_custom();
            echo $custom["phone"][0];
        break;
        case "date":
            $custom = get_post_custom();
            echo $custom["date"][0];
        break;
        case "bottles":
            $custom = get_post_custom();
            echo $custom["bottles"][0];
        break;

    }
}

function booktime_edit_columns($columns){
    $columns = array(
        "cb" => "<input type=\"checkbox\" />",
        "title" => "Post should show",
        "description" => "name",
        "address" => "address",
        "phone" => "phone",
        "date" => "date",
        "bottles" => "bottles"
    );

    return $columns;
}

?>

<?php
add_action("admin_init", "admin_init_booktime");

function admin_init_booktime(){

    add_meta_box("name", "name", "name", "booktime", "normal", "low");

}
?>

<?php
function name() {
    global $post;
    $custom = get_post_custom($post->ID);
    $name = ( !empty($custom["name"][0]) ) ? $custom["name"][0]: "";
    ?>
    <p><label>Name:</label><br />
        <input type="text" name="name" value="<?php echo $name; ?>"/></p>
    <?php
}
?>

<?php
add_action('save_post', 'save_details_booktime');

function save_details_booktime(){
    global $post;
    $custom_meta_fields = array( 'name' );

    foreach( $custom_meta_fields as $custom_meta_field ):
        if(isset($_POST[$custom_meta_field]) && $_POST[$custom_meta_field] != ""):
            update_post_meta($post->ID, $custom_meta_field, $_POST[$custom_meta_field]);
        endif;
    endforeach;

}
?>
share|improve this question
    
Im sorry if I don't follow. How do I update the meta, add it or delete it. –  Anders Kitson Jan 25 '12 at 7:55
    
Sorry to confuse you, disregard my last comment (I deleted it) as it was incorrect in accuracy anyways. See Bainternet's answer. –  Jared Jan 25 '12 at 18:10

2 Answers 2

up vote 2 down vote accepted

You need to replace the field name from name to something else since name is a reserved word and could be whats causing the blank page, also its better practice to prefix you field names.

Now in your save_details_booktime function make sure you are no the right post type and not in autosave, something like this:

 function save_details_booktime($post_id ) {
    global $post;   
    //skip auto save
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }
    /check for you post type only
    if( $post->post_type != "booktime" ) {
        return;

    }
    $custom_meta_fields = array( 'name' ); //change from name to something else

    foreach( $custom_meta_fields as $custom_meta_field ){
        if(isset($_POST[$custom_meta_field]) && $_POST[$custom_meta_field] != ""){
            update_post_meta($post_id, $custom_meta_field, $_POST[$custom_meta_field]);
        }
    }
}
share|improve this answer

The save_post action will return the ID of the post you're saving for you.

You may also want to verify the permissions the user has to update the post and make sure a task such as Autosave isn't running. I've added these in below.

(Also read Bainternet's answer)

add_action('save_post', 'save_details_booktime');

function save_details_booktime( $id ){

    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    // Check permissions
    if ( 'page' == $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_page', $id ) )
            return;
    } else {
        if ( !current_user_can( 'edit_post', $id ) )
            return;
    }

    $custom_meta_fields = array( 'some_name' );

    foreach( $custom_meta_fields as $custom_meta_field ):
        // Grab the existing meta data (will return an empty string if none)
        $meta_exists = get_post_meta( $id, $custom_meta_field, true );

        // If the meta doesn't match what was $_POST'ed
        if( $_POST[$custom_meta_field] != $meta_exists && $_POST[$custom_meta_field] != '' )
            update_post_meta( $id, $custom_meta_field, $_POST[$custom_meta_field] );

        // If the meta does exist, but the new data is blank, delete it
        if( $meta_exists != '' && $_POST[$custom_meta_field] == '' )
            delete_post_meta( $id, $custom_meta_field );

    endforeach;

}

Be sure to read the articles on get_post_meta, update_post_meta, delete_post_meta, and save_post for more information on the functions used above.

share|improve this answer
1  
the Codex says: (update_post_meta) may be used in place of add_post_meta() function. –  Stephen Harris Jan 25 '12 at 9:38
    
I need to pay attention better! :) Thanks lol –  Jared Jan 25 '12 at 17:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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