Custom meta box not saving

时间:2019-02-16 作者:Biily

我已经为自定义主题创建了一个自定义元框。将显示元框,但不保存任何输入的值

<?php
/*
@ PACKAGE 3KTECHS THEME
=========================
CUSTOM POST TYPE  PAGE
=========================

*/

$contact = get_option(\'activate_contact\');
if( @$contact == 1) {
   add_action(\'init\',\'ktechs_contact_custom_post_type\');

add_filter( \'manage_ktechs-contact_posts_columns\',\'ktechs_set_contact_columns\');
add_action( \'manage_ktechs-contact_posts_custom_column\',\'ktechs_contact_custom_column\', 10, 2);
add_action(\'add_meta_boxes\',\'ktechs_contact_add_meta_box\');
add_action(\'save-post\',\'ktechs_save_contact_email_data\');
}
/* custom contact cpt  */
function ktechs_contact_custom_post_type() {
    $labels = array(
        \'name\' =>\'Messages\',
        \'singular_name\' => \'Message\',
        \'menu_name\' => \'Messages\',
        \'name_admin_bar\' =>\'Message\',
    );
$args = array(
\'labels\' => $labels,
\'show_ui\' =>true,
\'show_in_menu\' => true,
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'menu_position\' => 26,
\'menu_icon\' => \'dashicons-email-alt\',
\'supports\' => array(\'title\',\'editor\', \'author\')
);
register_post_type(\'ktechs-contact\',$args);
}
function ktechs_set_contact_columns( $columns ){
    $newColumns = array();
    $newColumns[\'title\'] = \'Full Name\';
    $newColumns[\'message\'] = \'Message\';
    $newColumns[\'email\'] = \'Email\';
    $newColumns[\'date\'] = \'Date\';
    return $newColumns;
}

function ktechs_contact_custom_column( $column, $post_id ){

    switch( $column ){

        case \'message\' :
            echo get_the_excerpt();
            break;

        case \'email\' :
            //email column

            $email = get_post_meta( $post_id, \'_contact_email_value_key\', true );
            echo $email;
            break;
    }
}
/*CONTACT META BOXES */
function ktechs_contact_add_meta_box() {
    add_meta_box( \'contact_email\', \'User Email\', \'ktechs_contact_email_callback\', \'ktechs-contact\', \'side\' );
}

function ktechs_contact_email_callback( $post ) {
    wp_nonce_field( \'ktechs_save_contact_email_data\', \'ktechs_contact_email_meta_box_nonce\' );

    $value = get_post_meta( $post->ID, \'_contact_email_value_key\', true );

    echo \'<label for="ktechs_contact_email_field">User Email Address: </label>\';
    echo \'<input type="email" id="ktechs_contact_email_field" name="ktechs_contact_email_field" value="\' . esc_attr( $value ) . \'" size="25" />\';
}

function ktechs_save_contact_email_data( $post_id ) {

    if( ! isset( $_POST[\'ktechs_contact_email_meta_box_nonce\'] ) ){
        return;
    }

    if( ! wp_verify_nonce( $_POST[\'ktechs_contact_email_meta_box_nonce\'], \'ktechs_save_contact_email_data\') ) {
        return;
    }

    if( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ){
        return;
    }

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

    if( ! isset( $_POST[\'ktechs_contact_email_field\'] ) ) {
        return;
    }

    $my_data = sanitize_text_field( $_POST[\'ktechs_contact_email_field\'] );

    update_post_meta( $post_id, \'_contact_email_value_key\', $my_data );

}

1 个回复
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成

嗯,您发布了很多代码,问题的原因很容易解决(但很难找到)。

您的代码中有这一行:

add_action(\'save-post\',\'ktechs_save_contact_email_data\');
但是没有所谓的钩子save-post. 应该是这样的save_post. 因此,将上面的行更改为

add_action(\'save_post\', \'ktechs_save_contact_email_data\' );
它应该像一种魅力。

相关推荐

Starting with tabbed metabox

我的插件需要一个带选项卡的metabox。实际上,它们是5个代谢箱,每个都在一个选项卡中。我找到了以下脚本:https://speckyboy.com/10-simple-code-snippets-creating-beautiful-tabs/哪一个最适合在元框中使用?最好只有CSS?JS必须注册?我想开始测试这些脚本,但想知道从哪里开始。非常感谢。