jamie
jamie

Reputation: 545

Wordpress meta box in specific template file not saving

I seem to have a problem with my meta box saving, not totally sure what I've done wrong. Any help would be greatly appreciated!

Heres my functions file:

add_action( 'add_meta_boxes', 'cd_meta_box_add' );

function cd_meta_box_add()  
{
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
    $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);

    if ($template_file == 'golf.php')
    {
        add_meta_box ( 
            'golf-times', 
            'Golf Opening Times & Prices', 
            'cd_meta_box_cb', 
            'page', 
            'normal', 
            'high' 
        );
    }
}

function cd_meta_box_cb( $post )
{
    $values = get_post_custom( $post->ID );
    $times = isset( $values['golf_meta_box_times'] ) ? esc_attr( $values['golf_meta_box_times'][0] ) : '';
    $prices = isset( $values['golf_meta_box_prices'] ) ? esc_attr( $values['golf_meta_box_prices'][0] ) : '';
    wp_nonce_field( 'golf_meta_box_nonce', 'meta_box_nonce' );
    ?>
    <div style="overflow: hidden;">
        <div style="width: 45%; float: left;">
            <p><label for="golf_meta_box_times">Opening Times</label></p>
            <p><textarea type="text" name="golf_meta_box_times" id="golf_meta_box_times" rows="5" style="width: 90%;" value="<?php echo $times; ?>"> </textarea></p>
        </div>
        <div style="width: 45%; float: left;">
            <p><label for="golf_meta_box_prices">Prices</label></p>
            <p><textarea type="text" name="golf_meta_box_prices" id="golf_meta_box_prices" rows="5" style="width: 90%;" value="<?php echo $prices; ?>"> </textarea></p>
        </div>
    </div>
    <?php   
}


add_action( 'save_post', 'cd_meta_box_save' );
function cd_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'], 'golf_meta_box_nonce' ) ) return;

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

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

    if( isset( $_POST['golf_meta_box_times'] ) )
        update_post_meta( $post_id, 'golf_meta_box_times', wp_kses( $_POST['golf_meta_box_times'], $allowed ) );

    if( isset( $_POST['golf_meta_box_prices'] ) )
        update_post_meta( $post_id, 'golf_meta_box_prices', wp_kses( $_POST['golf_meta_box_prices'], $allowed ) );

}
?>

If anyone has a clue how to make this save it would be a great help!

I am also having issues making the data display - but I can only assume thats where it isnt actually saving haha!

Cheers

Upvotes: 0

Views: 418

Answers (1)

eric.itzhak
eric.itzhak

Reputation: 16062

functions.php cannot output HTML as far as i know. wrap your html in a string and return it, your file might be returning errors.

Your add_meta_box code seems to be correct, the condition might not be met, are you sure you are sending data over get?

Upvotes: 3

Related Questions