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 some text fields in a custom post type that I fill in at first, but need to delete on a future update of the post. When I delete the text in the field and save the post, the text I deleted comes back! If I enter a space, it saves OK. Does anybody know how I can fix this?

Here are the parts of the code for my custom post type that display and save the field data (didn't post the custom post type init code because it's right from the codex):

function book_info()
{
    global $post;
    $custom = get_post_custom($post->ID);
    $book_title = $custom["book_title"][0];
    ... (more field variables) ...
    ?>
    <p><label>Book Title:</label><br />
    <input class="medium" name="book_title" value="<?php echo esc_html($book_title); ?>" /></p>
    ... (more field display blocks) ...
    <?php
} 

add_action('save_post', 'save_book');
function save_book($post_id)
{
    // Verify this came from our screen with proper authorization.
    if ( !wp_verify_nonce( $_POST['book_info_noncename'], 'book_info'.$post_id )) {
        return $post_id;
    }

    // Determine whether this is an autosave routine. If so, our form has not been submitted and we don't want to do anything.
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return $post_id;

    // Check permissions
    if ( !current_user_can( 'edit_post', $post_id ) )
        return $post_id;

    // Now that authentication is complete, find and save the data.
    $post = get_post($post_id);
        if ($post->post_type == 'book') { 

            if (isset($_POST["book_title"]) && $_POST["book_title"] <> '') update_post_meta($post->ID, "book_title", $_POST["book_title"]);
            ... (more field save statements) ...

        return($_POST['book']);
    }
    return $post_id;
}

If I enter text in the "book_title" field and save, it works. If I then delete that text and save, the text is still there when the edit post screen refreshes after clicking save. If I delete that text and replace it with a space character, it saves. I need to be able to delete the text from the field without having to replace it with a space character. Any thoughts on how to accomplish this?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

The problematic line is this one:

if (isset($_POST["book_title"]) && $_POST["book_title"] <> '') update_post_meta($post->ID, "book_title", $_POST["book_title"]);

That logic says: If the submitted form contains a field called 'book_title' and if the content of the 'book_title' field does not equal an empty string, then save it. In your situation, the first condition should be met (HTML input fields with a 'text' type will appear in the $_POST global even when they're empty), but the second one is failing. Change it to:

if ( isset( $_POST["book_title"] ) ) 
    update_post_meta($post->ID, "book_title", $_POST["book_title"]);
share|improve this answer
    
Your solution works, and because you explained why, I also understand it. Thank you! I found that save method in a custom post type tutorial somewhere. Any idea why that code would be there in the first place? –  Joe Apr 15 '12 at 23:28
    
Glad to help! I assume that the code was originally there in order to prevent you from accidentally deleting an option. –  Boone Gorges Apr 16 '12 at 12:14
add comment

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.