$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>metabox - Custom meta box not saving|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

metabox - Custom meta box not saving

matteradmin9PV0评论

I've created a custom meta box for a custom theme. The metabox appears but does not save any inputed values

<?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 );

}

I've created a custom meta box for a custom theme. The metabox appears but does not save any inputed values

<?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 );

}
Share Improve this question edited Feb 16, 2019 at 21:24 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 16, 2019 at 21:20 BiilyBiily 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Well, there's a lot of code you've posted and the reason for the problem is pretty easy to fix (but hard to find).

You have this line in your code:

add_action('save-post','ktechs_save_contact_email_data');

But there is no hook called save-post. It should be save_post. So change the line above to

add_action('save_post', 'ktechs_save_contact_email_data' );

and it should work like a charm.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far