G
G
Grisha Nikolsky2015-12-18 22:45:02
WordPress
Grisha Nikolsky, 2015-12-18 22:45:02

Why is metabox custom field data not updating?

Hello. I create a custom metabox, but for some reason the data from it is not updated in the database.
Here I create the metabox itself:

function cgc_tour_meta_box() {
    add_meta_box(
        'tour_meta_box',
        'Tournament Meta Box',
        'cgc_display_meta_box', //callback
        'tour',
        'normal',
        'high'
    );
}

add_action( 'add_meta_boxes', 'cgc_tour_meta_box' );

There were no difficulties with this. The metabox is displayed.
I create a custom field for the metabox and write the value from the database there:
function cgc_display_meta_box( $post ) {
    $title = get_post_meta( $post -> ID, 'tour_title', true );
    ?>
            <!-- Название турнира -->
            <div class="meta-row">
                <div class="meta-th">
                    <label for="tour-title" class="cgc-row-title">Tournament Title</label>
                </div>
                <div class="meta-td">
                    <input type="text" id="tour-title" name="tour-title" value="<?php echo $title ?>">
                </div>
            </div>
<?php
}

This function is called when the metabox is created (see the first block of code).
Everything works great just the same. I created a field in the database with the key 'tour_title' and everything was displayed normally.
But the problem arose precisely in updating the data:
function cgc_meta_save( $post_id ) {

    if ( isset( $_POST['tour_title'] ) && $_POST['tour_title'] != '' ) {
        update_post_meta( $post_id, 'tour_title', sanitize_text_field( $_POST[ 'tour_title' ] ) );
    }
}
add_action( 'save_post', 'cgc_meta_save' );

They just don't update. No way. What could be wrong here? I rummaged through the documentation, everything seems to have done everything right, and it does not give any errors.
PS For the test, I made the simplest example (And that does not work). I'll fill it with all sorts of checks and nonce_fields later.
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WP Panda, 2015-12-18
@VoxelGod

Because the field name you have is tour - title and you are trying to save tour _ title

<input type="text" id="tour-title" name="tour_title" value="<?php echo $title ?>">

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question