# Recipe Details Meta Box

> Adds prep and cook times, servings, calories, ingredients and steps to recipes, plus a [recipe_card] shortcode that prints a tidy recipe card.

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

## Code

```php
// Post type, and the fields: meta key (saved with a leading underscore) => label and input type.
$wpide_box = array(
    'post_type' => 'recipe',
    'title'     => 'Recipe details',
    'fields'    => array(
        'recipe_prep' => array( 'label' => 'Prep time (minutes)', 'type' => 'number' ),
        'recipe_cook' => array( 'label' => 'Cook time (minutes)', 'type' => 'number' ),
        'recipe_servings' => array( 'label' => 'Servings', 'type' => 'number' ),
        'recipe_calories' => array( 'label' => 'Calories per serving', 'type' => 'number' ),
        'recipe_ingredients' => array( 'label' => 'Ingredients (one per line)', 'type' => 'textarea' ),
        'recipe_steps' => array( 'label' => 'Steps (one per line)', 'type' => 'textarea' ),
    ),
);

add_action( 'add_meta_boxes_' . $wpide_box['post_type'], function () use ( $wpide_box ) {
    add_meta_box( 'wpide-recipe-details', $wpide_box['title'], function ( $post ) use ( $wpide_box ) {
        wp_nonce_field( 'wpide_recipe_details', 'wpide_recipe_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_recipe_details_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['wpide_recipe_details_nonce'] ), 'wpide_recipe_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( 'recipe_card', function () {
    $id = get_the_ID();

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

    $get   = function ( $key ) use ( $id ) { return (string) get_post_meta( $id, '_recipe_' . $key, true ); };
    $lines = function ( $text ) { return array_filter( array_map( 'trim', explode( "\n", $text ) ) ); };
    $facts = array_filter( array(
        'Prep'     => $get( 'prep' ) ? (int) $get( 'prep' ) . ' min' : '',
        'Cook'     => $get( 'cook' ) ? (int) $get( 'cook' ) . ' min' : '',
        'Serves'   => $get( 'servings' ),
        'Calories' => $get( 'calories' ),
    ) );

    $html = '<div class="recipe-card">';

    if ( $facts ) {
        $html .= '<ul class="recipe-facts">';
        foreach ( $facts as $label => $value ) {
            $html .= '<li><strong>' . esc_html( $label ) . ':</strong> ' . esc_html( $value ) . '</li>';
        }
        $html .= '</ul>';
    }

    if ( $lines( $get( 'ingredients' ) ) ) {
        $html .= '<h3>Ingredients</h3><ul class="recipe-ingredients"><li>' . implode( '</li><li>', array_map( 'esc_html', $lines( $get( 'ingredients' ) ) ) ) . '</li></ul>';
    }

    if ( $lines( $get( 'steps' ) ) ) {
        $html .= '<h3>Method</h3><ol class="recipe-steps"><li>' . implode( '</li><li>', array_map( 'esc_html', $lines( $get( 'steps' ) ) ) ) . '</li></ol>';
    }

    return $html . '</div>';
} );
```

## Notes

Enter one ingredient or step per line. The Recipe schema snippet reads these fields.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Recipe 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/
