0

I created a client profile custom post type and have started creating the meta boxes and such for users to enter in information. While all of that seems to be working fine, when I go to save the custom post, nothing gets sent to the database. Here are my functions

Meta Box

function add_meta_boxes() {
    add_meta_box(
        'client-info',
        __( 'Client Information', 'textdomain' ),
        'meta_box_clientinfo_callback',
        'clientprofile',
        'normal',
        'high'
    );
}
add_action( 'add_meta_boxes', 'add_meta_boxes' );

Client Info Meta Box Callback

function meta_box_clientinfo_callback( $post )  {
    global $my_post_meta;
    $my_post_meta = get_post_meta( $post->ID );
    wp_nonce_field( plugin_basename( __FILE__ ), 'my-nonce' );
?>

    <p>Please enter in the information on the client whom you wish to feature in a case story.</p>
    <p>
        <label for="client-name"><?php _e( 'Client Name', 'textdomain' ); ?></label>
        <input type="text" name="client-name" id="client-name" value="<?php field_value( 'client-name' ); ?>" size="25" />
    </p>

    <p>
        <label for="client-age"><?php _e( 'Client Age', 'textdomain' ); ?></label>
        <input type="text" name="client-age" id="client-age" value="<?php field_value( 'client-age' ); ?>" size="3" maxlength="3" />
    </p>

    <p>
        <label for="client-gender"><?php _e( 'Client Gender', 'textdomain' ); ?></label>
        <select id="client-gender" name="client-gender">
            <option value="male">Male</option>
            <option value="female">Female</option>
            <option value="Other">Other</option>
        </select>
    </p>

<?php
}

Save Post

function save_meta( $post_id ) {

    // Verify Nonce
    if ( ! isset( $_POST['my-nonce'] ) || ! wp_verify_nonce( $_POST['my-nonce'], basename( __FILE__ ) ) ) {
        return;
    }

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

    if ( isset( $_REQUEST['client-name'] ) ) {
        update_post_meta( $post_id, '_client_name', sanitize_text_field( $_POST{'client-name'} ) );
    }

}
add_action( 'save_post'. 'save_meta', 10, 2 );

Field_Value Function

function field_value( $field_id ) {

    global $post_meta;

    if ( isset ( $post_meta[$field_id] ) ) {
        echo $post_meta[$field_id][0];
    }
}

What am I missing something here? I looked at several examples online to get the code I have now but when I look in PHPMyAdmin at the wp_postmeta table for the Post ID, I only see keys for _edit_last and _edit_lock.

2

The primary issue is your save_post action isn't firing because:

add_action( 'save_post'. 'save_meta', 10, 2 );

should be

add_action( 'save_post', 'save_meta', 10, 2 );

You also have inconsistency between client-name and _client_name, but without seeing the field_value function, I'm not sure if that's a problem or not.

I would also use something more unique than save_meta and add_meta_boxes for function names, potential for collision with some other code is high.

7
  • I removed the name spacing to make for simpler reading and shorter lines of code. I did change the . to a ,, but am still seeing just the two keys in wp_postmeta from before, even on a new post. – Cedon May 26 '17 at 1:35
  • A copy/paste of your code works for me, as far as saving is concerned. You've still got some naming issues with your field_value function, so you won't see the saved values in your form field, but it does end up in the database. – Milo May 26 '17 at 2:13
  • Can you copy what you had into pastebin or something so I can compare? It's still not working for me. – Cedon May 26 '17 at 2:43
  • Here it is. I changed the post type to post in add_meta_box, and added a print_r($my_post_meta) in meta_box_clientinfo_callback to show all the saved meta data. – Milo May 26 '17 at 2:52
  • Still unable to get it to work with the custom post type. I even tried using the 'save_post_mycpt' hook for add_action() per the save_post_{$post->post_type} hook that was added in 3.7 – Cedon May 26 '17 at 3:25

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.