'Wordpress - Add custom field to custom post created programmatically

I have created a custom plugin on Wordpress that creates a custom post on installation. The code I use is as follows:

function custom_post_type() {

$labels = array(
    'name'                => _x( 'Books', 'Post Type General Name', 'my-custom-domain' ),
    'singular_name'       => _x( 'Book', 'Post Type Singular Name', 'my-custom-domain' ),
    'menu_name'           => __( 'Book', 'my-custom-domain' ),
    'parent_item_colon'   => __( 'Parent Book', 'my-custom-domain' ),
    'all_items'           => __( 'All Books', 'my-custom-domain' ),
    'view_item'           => __( 'Show Book', 'my-custom-domain' )
);

$args = array(
    'label'               => __( 'Books', 'my-custom-domain' ),
    'description'         => __( 'Books and news', 'my-custom-domain' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
    'taxonomies'          => array( 'genres' ),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'post',
    'show_in_rest' => true,

);

register_post_type( 'libri', $args ); 
}
add_action( 'init', 'custom_post_type', 0 );

What I would like to do is to be able to create a custom field (again from code) to be inserted under this function and add it to the post created above.

A custom field such as a text field with a title and a label box in which to insert text, or a field consisting of two text boxes.

How can I create it in code and add it to the post so that when I do "Add new" I get this field to fill in?

I mean something like THIS.



Solution 1:[1]

There's some really great tutorials that cover this. My answer is taken from this one written by Aileen Javier from WPMU Dev

It's a 3 step process to create custom metaboxes on a page. The first step is to create the metabox using the add_meta_box() function

function libri_add_meta_box( $post ){
    add_meta_box( 'libri_meta_box', 'Book Attributes', 'libri_build_meta_box', 'libri', 'side', 'low' );
}
add_action( 'add_meta_boxes_libri', 'libri_add_meta_box' );

The next step is to create a function which we use as the callback parameter in our add_meta_box (in this case 'libri_build_meta_box') - We use this to create inputs for our custom fields. In this instance a text field for Book Author.

function libri_build_meta_box( $post ){
    wp_nonce_field( basename( __FILE__ ), 'libri_meta_box_nonce' );
  $book_author = get_post_meta( $post->ID, '_book_author', true); ?>
  <div class='inside'>
    <h3>Book Author</h3>
    <p>
        <input type="text" name="book_author" value="<?php echo $book_author; ?>" /> 
    </p>
  </div>
<?php }

And the final step is to hook into the save_post_{$post->post_type} action, to take the request and then update the post_meta when the post is saved

function libri_save_meta_box_data( $post_id ){
  if ( !isset( $_POST['food_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['food_meta_box_nonce'], basename( __FILE__ ) ) ){
    return;
  }

  if ( isset( $_REQUEST['book_author'] ) ) {
    update_post_meta( $post_id, '_book_author', sanitize_text_field( $_POST['book_author'] ) );
  }
}
add_action( 'save_post_libri', 'libri_save_meta_box_data', 10, 2 );

This will create a metabox on the side of the page that has a single text input for a book author. I'd really recommend reading the whole tutorial from wpmudev, because it's really informative and helpful.

Here's the full code from my example:

<?php function libri_add_meta_box( $post ){
    add_meta_box( 'libri_meta_box', 'Book Attributes', 'libri_build_meta_box', 'libri', 'side', 'low' );
}
add_action( 'add_meta_boxes_libri', 'libri_add_meta_box' );

function libri_build_meta_box( $post ){
    wp_nonce_field( basename( __FILE__ ), 'libri_meta_box_nonce' );
  $book_author = get_post_meta( $post->ID, '_book_author', true); ?>
  <div class='inside'>
    <h3>Book Author</h3>
    <p>
        <input type="text" name="book_author" value="<?php echo $book_author; ?>" /> 
    </p>
  </div>
<?php }


function libri_save_meta_box_data( $post_id ){
  if ( !isset( $_POST['food_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['food_meta_box_nonce'], basename( __FILE__ ) ) ){
    return;
  }

  if ( isset( $_REQUEST['book_author'] ) ) {
    update_post_meta( $post_id, '_book_author', sanitize_text_field( $_POST['book_author'] ) );
  }
}
add_action( 'save_post_libri', 'libri_save_meta_box_data', 10, 2 );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jon Walter