-1

I can't save data from a select metabox. My code is

http://pastebin.com/BMmKzti2

Where I'm going wrong?

Edit:

Here is my code

    function fichasvtmbasicas_meta_box_callback( $post ) {
    wp_nonce_field( 'fichasvtmbasicas_meta_box', 'fichasvtmbasicas_meta_box_nonce' );
    $fichasvtmbasicas_geracao_field = get_post_meta( $post->ID, '_vtmbasica_geracao_value_key', true );
    $selecionargeracao = isset( $fichasvtmbasicas_geracao_field['_vtmbasica_geracao_value_key'] ) ? esc_attr( $fichasvtmbasicas_geracao_field['_vtmbasica_geracao_value_key'][0] ) : '';

    echo '<div class="unidadeelementotopovtm"><label for="fichasvtmbasicas_geracao_field">';
    _e( 'Geração:', 'fichasvtmbasicas_textdomain' );
    echo '</label> ';
    echo '<select id="fichasvtmbasicas_geracao_field" name="fichasvtmbasicas_geracao_field">';
    echo '<option value="geracao15"' . selected( $selecionargeracao['_vtmbasica_geracao_value_key'], '15' ) . '>15</option>';
    echo '<option value="geracao14"' . selected( $selecionargeracao['_vtmbasica_geracao_value_key'], '14' ) . '>14</option>';
    echo '<select>';
    echo '</div>';
    }

    function fichasvtmbasicas_save_meta_box_data( $post_id ) {
    if ( ! isset( $_POST['fichasvtmbasicas_meta_box_nonce'] ) ) {
            return;
    }
    if ( ! wp_verify_nonce( $_POST['fichasvtmbasicas_meta_box_nonce'], 'fichasvtmbasicas_meta_box' ) ) {
            return;
    }
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return;
    }
    if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                    return;
            }

    } else {

            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                    return;
            }
    }
    if ( ! isset( $_POST['fichasvtmbasicas_geracao_field'] ) ) { //para Geração
            return;
    }
    update_post_meta( $post_id, '_vtmbasica_geracao_value_key', esc_attr ( $_POST[ 'fichasvtmbasicas_geracao_field' ] )); //para Geração
    }

    add_action( 'save_post', 'fichasvtmbasicas_save_meta_box_data' );

Edit:

I changed my code for

function fichasvtmbasicas_meta_box_callback( $post ) {
wp_nonce_field( 'fichasvtmbasicas_meta_box', 'fichasvtmbasicas_meta_box_nonce' );
$selecionargeracao = isset( $fichasvtmbasicas_geracao_field['fichasvtmbasicas_geracao_field'] ) ? esc_attr( $fichasvtmbasicas_geracao_field['fichasvtmbasicas_geracao_field'][0] ) : '';
echo '<div class="unidadeelementotopovtm"><label for="fichasvtmbasicas_geracao_field">';
_e( 'Geração:', 'fichasvtmbasicas_textdomain' );
echo '</label> ';
echo '<select id="fichasvtmbasicas_geracao_field" name="fichasvtmbasicas_geracao_field">';
echo '<option value="geracao15"' . selected( $selecionargeracao, '15' ) . '>15</option>';
echo '<option value="geracao14"' . selected( $selecionargeracao, '14' ) . '>14</option>';
echo '<select>';
echo '</div>';

function fichasvtmbasicas_save_meta_box_data( $post_id ) {
if ( ! isset( $_POST['fichasvtmbasicas_meta_box_nonce'] ) ) {
    return;
}

// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['fichasvtmbasicas_meta_box_nonce'], 'fichasvtmbasicas_meta_box' ) ) {
    return;
}

// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return;
}

// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

    if ( ! current_user_can( 'edit_page', $post_id ) ) {
        return;
    }

} else {

    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }
}
    if ( ! isset( $_POST['fichasvtmbasicas_geracao_field'] ) ) { //para Geração
    return;
}
update_post_meta( $post_id, 'fichasvtmbasicas_geracao_field', esc_attr ( $_POST[ 'fichasvtmbasicas_geracao_field' ] ));
}
add_action( 'save_post', 'fichasvtmbasicas_save_meta_box_data' );

but still can't save my data. I have more metaboxes on the same function and they are saving the data, just the select don't save it.

1
  • Please add all the relevant code directly to your question. Do not use pastebin. The problem is, if that paste is removed, your question will be useless to others. Also see How to Ask – Pieter Goosen Aug 4 '14 at 15:38
0

I got the answer. Now I have the code

function fichasvtmbasicas_meta_box_callback( $post ) {
wp_nonce_field( 'fichasvtmbasicas_meta_box', 'fichasvtmbasicas_meta_box_nonce' );
$fichasvtmbasicas_geracao_field = get_post_meta( $post->ID, '_vtmbasica_geracao_value_key', true );

echo '<div class="unidadeelementotopovtm"><label for="fichasvtmbasicas_geracao_field" class="topodaficha_titles">';
_e( 'Geração:', 'fichasvtmbasicas_textdomain' );
echo '</label> ';
echo '<select id="fichasvtmbasicas_geracao_field" name="fichasvtmbasicas_geracao_field">';
echo '<option value="decima-quinta" ' . selected( $fichasvtmbasicas_geracao_field, 'decima-quinta' ) . '>15ª</option>';
echo '<option value="decima-quarta" ' . selected( $fichasvtmbasicas_geracao_field, 'decima-quarta' ) . '>14ª</option>';
echo '<option value="decima-terceira" ' . selected( $fichasvtmbasicas_geracao_field, 'decima-terceira' ) . '>13ª</option>';
echo '<option value="decima-segunda" ' . selected( $fichasvtmbasicas_geracao_field, 'decima-segunda' ) . '>12ª</option>';
echo '<option value="decima-primeira" ' . selected( $fichasvtmbasicas_geracao_field, 'decima-primeira' ) . '>11ª</option>';
echo '<option value="decima" ' . selected( $fichasvtmbasicas_geracao_field, 'decima' ) . '>10ª</option>';
echo '<option value="nona" ' . selected( $fichasvtmbasicas_geracao_field, 'nona' ) . '>9ª</option>';
echo '<option value="oitava" ' . selected( $fichasvtmbasicas_geracao_field, 'oitava' ) . '>8ª</option>';
echo '<select>';
echo '</div>';
}

Then

function fichasvtmbasicas_save_meta_box_data( $post_id ) {
if ( ! isset( $_POST['fichasvtmbasicas_meta_box_nonce'] ) ) {
    return;
}
if ( ! wp_verify_nonce( $_POST['fichasvtmbasicas_meta_box_nonce'], 'fichasvtmbasicas_meta_box' ) ) {
    return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return;
}
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

    if ( ! current_user_can( 'edit_page', $post_id ) ) {
        return;
    }

} else {

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

if ( ! isset( $_POST['fichasvtmbasicas_geracao_field'] ) ) { //para Geração
    return;
}

$vtmbasica_geracao = sanitize_text_field( $_POST['fichasvtmbasicas_geracao_field'] ); //para Geração
update_post_meta( $post_id, '_vtmbasica_geracao_value_key', $vtmbasica_geracao ); //para Geração
}

add_action( 'save_post', 'fichasvtmbasicas_save_meta_box_data' );

And it's solved now. It can be useful for someone.

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.