1

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

}
0

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.

  • i had assumed I had a syntax error, mistype, or spelling error but could not find it. Thank you for your help it now works "like a charm' – Biily Feb 16 at 23:44

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.