Add special meta box to custom post type

The whole bunch of code is to replicate the process, not to give the actual code. You have to figure out how your process can be meet up with this scenario. I commented on every possible line, please see the inline comments to get proper understanding.

Step #1

First of all, the first portion of your code makes a Custom Meta Box, so use the portion to make your meta box first:

<?php
//making the meta box (Note: meta box != custom meta field)
function wpse_add_custom_meta_box_2() {
   add_meta_box(
       'custom_meta_box-2',       // $id
       'Dauer2',                  // $title
       'show_custom_meta_box_2',  // $callback
       'project',                 // $page
       'normal',                  // $context
       'high'                     // $priority
   );
}
add_action('add_meta_boxes', 'wpse_add_custom_meta_box_2');
?>

Step #2

So your meta box is ready now. Now you have to make some Form fields to get user data, and for that we are using the $callback function what we just declared:

<?php
//showing custom form fields
function show_custom_meta_box_2() {
    global $post;

    // Use nonce for verification to secure data sending
    wp_nonce_field( basename( __FILE__ ), 'wpse_our_nonce' );

    ?>

    <!-- my custom value input -->
    <input type="number" name="wpse_value" value="">

    <?php
}
?>

Step #3

On the post save the two fields will post values, now we have to save ’em where we want to save ’em.

<?php
//now we are saving the data
function wpse_save_meta_fields( $post_id ) {

  // verify nonce
  if (!isset($_POST['wpse_our_nonce']) || !wp_verify_nonce($_POST['wpse_our_nonce'], basename(__FILE__)))
      return 'nonce not verified';

  // check autosave
  if ( wp_is_post_autosave( $post_id ) )
      return 'autosave';

  //check post revision
  if ( wp_is_post_revision( $post_id ) )
      return 'revision';

  // check permissions
  if ( 'project' == $_POST['post_type'] ) {
      if ( ! current_user_can( 'edit_page', $post_id ) )
          return 'cannot edit page';
      } elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
          return 'cannot edit post';
  }

  //so our basic checking is done, now we can grab what we've passed from our newly created form
  $wpse_value = $_POST['wpse_value'];

  //simply we have to save the data now
  global $wpdb;

  $table = $wpdb->base_prefix . 'project_bids_mitglied';

  $wpdb->insert(
            $table,
            array(
                'col_post_id' => $post_id, //as we are having it by default with this function
                'col_value'   => intval( $wpse_value ) //assuming we are passing numerical value
              ),
            array(
                '%d', //%s - string, %d - integer, %f - float
                '%d', //%s - string, %d - integer, %f - float
              )
          );

}
add_action( 'save_post', 'wpse_save_meta_fields' );
add_action( 'new_to_publish', 'wpse_save_meta_fields' );
?>

As you are dealing with custom table, so we are sticking with $wpdb class to securely store necessary data.

Please note, this is not the code what you require, this is the idea and the process how you can now shape your path. Just remember two things:

  1. show_custom_meta_box_2() is the area where your HTML form is, treat this part like a simple HTML form, and
  2. wpse_save_meta_fields() hooked to necessary actions, will save the data of the form when the post is published / saved / updated. Take proper sanitization and validation here.

Hope it helps. Thanks to @toscho for the recent chat. <3

Leave a Comment