0

So I created a few custom metaboxes like this, and they all operate this way. They work perfectly the first time I put anything into them, and any time I edit them after that, but if they are emptied, they will no longer update.

add_action( 'admin_menu', 'TFP_meta' );
    add_action( 'save_post', 'save_TFP_meta', 10, 2 );

    function TFP_meta() {
        add_meta_box( 'TFP-meta-box', 'Toll Free', 'TFP_meta_box', 'page', 'side', 'low' );
    }

    function TFP_meta_box( $object, $box ) { ?>
        <p>
            <br />
            <textarea name="TFP" id="TFP" cols="60" rows="4" tabindex="30" style="width: 97%;"><?php echo wp_specialchars( get_post_meta( $object->ID, 'TFP', true ), 1 ); ?></textarea>
            <input type="hidden" name="TFP_meta_box_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
            <script type="text/javascript">edCanvas = document.getElementById('TFP');edInsertContent = null;</script>
        </p>
    <?php }

    function save_TFP_meta( $post_id, $post ) {

        if ( !wp_verify_nonce( $_POST['TFP_meta_box_nonce'], plugin_basename( __FILE__ ) ) )
            return $post_id;

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

        $TFP_meta_value = get_post_meta( $post_id, 'TFP', true );
        $new_TFP_meta_value = stripslashes( $_POST['TFP'] );

        if ( $new_TFP_meta_value && '' == $TFP_meta_value )
            add_post_meta( $post_id, 'TFP', $new_TFP_meta_value, true );

        elseif ( $new_TFP_meta_value != $TFP_meta_value )
            update_post_meta( $post_id, 'TFP', $new_TFP_meta_value );

        elseif ( '' == $new_TFP_meta_value && $TFP_meta_value )
            delete_post_meta( $post_id, 'TFP', $TFP_meta_value );
    }

What could be causing this? It seems like maybe just some small bug, or is something in my saving code incorrect?

1 Answer 1

1

Here's an updated version of save_TFP_meta(), which has modified saving logic. It fixes the issue where the meta data was not being saved after being emptied.

function save_TFP_meta( $post_id, $post ) {

    if ( ! wp_verify_nonce( $_POST['TFP_meta_box_nonce'], plugin_basename( __FILE__ ) ) )
            return $post_id;

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

    $TFP_meta_value = get_post_meta( $post_id, 'TFP', true );
    $new_TFP_meta_value = stripslashes( $_POST['TFP'] );

    if ( false !== $TFP_meta_value && '' == $new_TFP_meta_value ) {
        delete_post_meta( $post_id, 'TFP', $TFP_meta_value );
    } else {
        update_post_meta( $post_id, 'TFP', $new_TFP_meta_value );
    }
}
1
  • 1
    Trenton Moore, as a side note, sanitize your value before insertion if you didn't already in your original code...
    – ClemC
    Commented Oct 5, 2016 at 21:20

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.