0

I'm troubled with a custom metabox, could someone give me a helping hand? I've got a custom post type which uses it, but unfortunately it's not storing the data into the database.

Any clue?

   add_action( 'add_meta_boxes', 'beef_meta_box_add' );
    function beef_meta_box_add()
    {
add_meta_box( 'beef-metabox', 'Custom Product Settings', 'beef_meta_box_cb', 'product', 'normal', 'high' );}

    function beef_meta_box_cb()
    {
 global $post;
$values = get_post_custom( $post->ID );
$price = isset( $values['beef_meta_box_price'] ) ? $values['beef_meta_box_price'] : '';
     wp_nonce_field( 'beef_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
    <label for="beef_meta_box_price">price n stuff</label>
    <input type="text" name="beef_meta_box_price" id="beef_meta_box_price" value="<?php echo $price; ?>" />
</p>
<?php    
    }

    add_action( 'save_post', 'beef_meta_box_save' );

    function beef_meta_box_save( $post_id )
    {
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'beef_meta_box_nonce' ) ) return;

if( !current_user_can( 'edit_post' ) ) return;

$allowed = array( 
    'a' => array(
        'href' => array()
    )
);

if( isset( $_POST['beef_meta_box_price'] ) )
    update_post_meta( $post_id, 'beef_meta_box_price', wp_kses( $_POST['beef_meta_box_price'], $allowed ) );
             }
0

Are you sure it isn't saving the values to the DB? Perhaps it's just not displaying the previously saved values in your form?

$values = get_post_custom( $post->ID );

This is what you are using to get your meta - but this returns a multi-dimensional array. Therefore when accessing your beef_meta_box_price value, you need to use $values['beef_meta_box_price'][0]. Alternatively, replace get_post_custom (which returns all meta for the post) with get_post_meta:

$beef_meta_box_price = get_post_custom( $post->ID, 'beef_meta_box_price', true );

This way you don't get an array, just a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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