关于从CPT SELECT Metabox保存数据的问题

时间:2014-08-04 作者:Eva Morrissey

我无法保存select metabox中的数据。我的代码是

http://pastebin.com/BMmKzti2

我错在哪里?

编辑:

这是我的密码

    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\' );
编辑:

我更改了的代码

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\' );
但仍然无法保存我的数据。我在同一个函数上有更多的元数据库,它们正在保存数据,只需选择“不保存”。

1 个回复
最合适的回答,由SO网友:Eva Morrissey 整理而成

我得到了答案。现在我有密码了

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>\';
}
那么

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\' );
现在问题解决了。它可能对某人有用。

结束

相关推荐

将字符计数添加到自定义Metabox

我知道有一种方法可以将字符计数器添加到WP中的摘录元盒,但我想将字符计数器添加到自定义元盒。下面的脚本将获取元数据库中的计数器,元数据库中有选项卡,但不计数。任何帮助都会很好。谢谢,这是我在StackExchange中的第一个问题。jQuery(document).ready(function(){ jQuery(\"#bgnp-metabox-tabs\").after(\"<p style=\\\"text-align:left;\\\"><small>Exce