# Book Details Meta Box

> Adds author, ISBN, publisher, publication date, page count and a buy link to books, plus a [book_details] shortcode.

- Type: PHP
- Runs: Run everywhere
- Category: [Custom Post Types & Fields](https://wpide.com/library/category/post-types/)
- Tags: meta-box, books, publishing
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/metabox-book-details/

## Code

```php
// Post type, and the fields: meta key (saved with a leading underscore) => label and input type.
$wpide_box = array(
    'post_type' => 'book',
    'title'     => 'Book details',
    'fields'    => array(
        'book_author_name' => array( 'label' => 'Author', 'type' => 'text' ),
        'book_isbn' => array( 'label' => 'ISBN', 'type' => 'text' ),
        'book_publisher' => array( 'label' => 'Publisher', 'type' => 'text' ),
        'book_published' => array( 'label' => 'Published', 'type' => 'date' ),
        'book_pages' => array( 'label' => 'Pages', 'type' => 'number' ),
        'book_buy_url' => array( 'label' => 'Buy link', 'type' => 'url' ),
    ),
);

add_action( 'add_meta_boxes_' . $wpide_box['post_type'], function () use ( $wpide_box ) {
    add_meta_box( 'wpide-book-details', $wpide_box['title'], function ( $post ) use ( $wpide_box ) {
        wp_nonce_field( 'wpide_book_details', 'wpide_book_details_nonce' );
        echo '<div style="display:grid;gap:12px;grid-template-columns:repeat(auto-fill,minmax(220px,1fr))">';

        foreach ( $wpide_box['fields'] as $key => $field ) {
            $value = get_post_meta( $post->ID, '_' . $key, true );
            $id    = 'wpide-' . str_replace( '_', '-', $key );
            echo '<p style="margin:0">';

            if ( 'checkbox' === $field['type'] ) {
                printf( '<label><input type="checkbox" name="%1$s" value="1"%2$s> %3$s</label>', esc_attr( $key ), checked( $value, '1', false ), esc_html( $field['label'] ) );
            } else {
                printf( '<label for="%1$s"><strong>%2$s</strong></label><br>', esc_attr( $id ), esc_html( $field['label'] ) );

                if ( 'textarea' === $field['type'] ) {
                    printf( '<textarea class="widefat" rows="5" id="%1$s" name="%2$s">%3$s</textarea>', esc_attr( $id ), esc_attr( $key ), esc_textarea( $value ) );
                } elseif ( 'select' === $field['type'] ) {
                    printf( '<select class="widefat" id="%1$s" name="%2$s">', esc_attr( $id ), esc_attr( $key ) );
                    foreach ( $field['options'] as $option => $label ) {
                        printf( '<option value="%1$s"%2$s>%3$s</option>', esc_attr( $option ), selected( (string) $value, (string) $option, false ), esc_html( $label ) );
                    }
                    echo '</select>';
                } else {
                    printf( '<input class="widefat" type="%1$s" id="%2$s" name="%3$s" value="%4$s"%5$s>', esc_attr( $field['type'] ), esc_attr( $id ), esc_attr( $key ), esc_attr( $value ), 'number' === $field['type'] ? ' step="any" min="0"' : '' );
                }
            }

            echo '</p>';
        }

        echo '</div>';
    }, $wpide_box['post_type'], 'normal', 'high' );
} );

add_action( 'save_post_' . $wpide_box['post_type'], function ( $post_id ) use ( $wpide_box ) {
    if ( ! isset( $_POST['wpide_book_details_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['wpide_book_details_nonce'] ), 'wpide_book_details' ) ) {
        return;
    }

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

    foreach ( $wpide_box['fields'] as $key => $field ) {
        $raw = isset( $_POST[ $key ] ) ? wp_unslash( $_POST[ $key ] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- sanitized below per type.
        $raw = is_string( $raw ) ? $raw : '';

        switch ( $field['type'] ) {
            case 'url':
                $value = esc_url_raw( $raw );
                break;
            case 'email':
                $value = sanitize_email( $raw );
                break;
            case 'number':
                $value = is_numeric( $raw ) ? (string) ( 0 + $raw ) : '';
                break;
            case 'textarea':
                $value = sanitize_textarea_field( $raw );
                break;
            case 'checkbox':
                $value = $raw ? '1' : '';
                break;
            case 'select':
                $value = array_key_exists( $raw, $field['options'] ) ? $raw : '';
                break;
            case 'date':
                $value = preg_match( '/^\d{4}-\d{2}-\d{2}$/', $raw ) ? $raw : '';
                break;
            case 'datetime-local':
                $value = preg_match( '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/', $raw ) ? $raw : '';
                break;
            default:
                $value = sanitize_text_field( $raw );
        }

        if ( '' === $value ) {
            delete_post_meta( $post_id, '_' . $key );
        } else {
            update_post_meta( $post_id, '_' . $key, $value );
        }
    }
} );

add_shortcode( 'book_details', function () {
    $id = get_the_ID();

    if ( ! $id || 'book' !== get_post_type( $id ) ) {
        return '';
    }

    $get  = function ( $key ) use ( $id ) { return (string) get_post_meta( $id, '_book_' . $key, true ); };
    $rows = array_filter( array(
        'Author'    => $get( 'author_name' ),
        'Publisher' => $get( 'publisher' ),
        'Published' => $get( 'published' ) ? wp_date( get_option( 'date_format' ), strtotime( $get( 'published' ) . ' 12:00' ) ) : '',
        'Pages'     => $get( 'pages' ),
        'ISBN'      => $get( 'isbn' ),
    ) );

    $html = '';

    foreach ( $rows as $label => $value ) {
        $html .= '<dt>' . esc_html( $label ) . '</dt><dd>' . esc_html( $value ) . '</dd>';
    }

    $buy = $get( 'buy_url' ) ? '<p><a class="wp-block-button__link wp-element-button" href="' . esc_url( $get( 'buy_url' ) ) . '" rel="sponsored noopener">Buy the book</a></p>' : '';

    return ( $html ? '<dl class="book-details">' . $html . '</dl>' : '' ) . $buy;
} );
```

## Notes

Made for the Books post type snippet.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Book Details Meta Box" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
