Support » Fixing WordPress » Trouble displaying saved custom field value on edit screen

  • I created a plugin to support a “Beers” post type for a brewery client. I initially added custom field for ABV and IBU with success.

    My issue popped up when I tried to extend it to include fields for “Food Pairings” and “Serving Temperature”

    Entering the values for the first time works smoothly, and the informtion shows up where I’d like it to on the site – however when I go to update an entry, the fields for pairing and temperature do not reflect the value that was most recently saved (either blank or ‘0’).

    Obviously, I’d like those values to remain intact, so when my client makes a revision to the beer’s description, the food pairing and serving temperature wont be wiped out when ‘update’ is pressed.

    My plugin’s code is below:

    <?php
    /*
    Plugin Name: No Label Beers
    Plugin URI: http://oakinteractive.com/
    Description: Declares a plugin that will create a custom post type displaying beers.
    Version: 1.0
    Author: Oak Interactive
    Author URI: http://oakinteractive.com/
    License: GPLv2
    */
    
    add_action( 'init', 'create_beer_info' );
    
    function create_beer_info() {
        register_post_type( 'nl_beers',
            array(
                'labels' => array(
                    'name' => 'Beers',
                    'singular_name' => 'Beer',
                    'add_new' => 'Add New',
                    'add_new_item' => 'Add New Beer',
                    'edit' => 'Edit',
                    'edit_item' => 'Edit Beer',
                    'new_item' => 'New Beer',
                    'view' => 'View',
                    'view_item' => 'View Beer',
                    'search_items' => 'Search Beers',
                    'not_found' => 'No Beer found',
                    'not_found_in_trash' => 'No Beers found in Trash',
                    'parent' => 'Parent Beer'
                ),
    
                'public' => true,
                'menu_position' => 15,
                'supports' => array( 'title', 'editor', 'comments', 'thumbnail'),
                'taxonomies' => array( '' ),
                'menu_icon' => plugins_url( 'images/image.png', __FILE__ ),
                'has_archive' => true
            )
        );
    }
    
    add_action( 'admin_init', 'my_admin' );
    
    function my_admin() {
        add_meta_box( 'beer_info_meta_box',
            'Beer Details',
            'display_beer_info_meta_box',
            'nl_beers', 'normal', 'high'
        );
    }
    
    function display_beer_info_meta_box( $beer_info ) {
    
        $beer_abv = esc_html( get_post_meta( $beer_info->ID, 'beer_abv', true ) );
        $beer_pair = intval( get_post_meta( $beer_info->ID, 'beer_pair', true ) );
        $beer_ibu = intval( get_post_meta( $beer_info->ID, 'beer_ibu', true ) );
        $beer_temp = intval( get_post_meta( $beer_info->ID, 'beer_temp', true ) );
        ?>
        <table>
            <tr>
                <td style="width: 150px">ABV</td>
                <td><input style="width:200px;" type="text" size="80" name="beer_info_abv" value="<?php echo $beer_abv; ?>" /></td>
            </tr>
            <tr>
                <td style="width: 150px">IBU</td>
                <td><input style="width:200px;" type="text" size="80" name="beer_info_ibu" value="<?php echo $beer_ibu; ?>" /></td>
            </tr>
            <tr>
                <td style="width: 150px">Food Pairing</td>
                <td><input style="width:200px;" type="text" size="80" name="beer_info_pair" value="<?php echo $beer_pair; ?>" /></td>
            </tr>
            <tr>
                <td style="width: 150px">Serving Temp</td>
                <td><input style="width:200px;" type="text" size="80" name="beer_info_temp" value="<?php echo $beer_temp; ?>" />&deg;F</td>
            </tr>
    
        </table>
        <?php
    }
    
    add_action( 'save_post', 'add_beer_info_fields', 10, 2 );
    
    function add_beer_info_fields( $beer_info_id, $beer_info ) {
        if ( $beer_info->post_type == 'nl_beers' ) {
            // Store data in post meta table if present in post data
            if ( isset( $_POST['beer_info_abv'] ) && $_POST['beer_info_abv'] != '' ) {
                update_post_meta( $beer_info_id, 'beer_abv', $_POST['beer_info_abv'] );
            }
            if ( isset( $_POST['beer_info_temp'] ) && $_POST['beer_info_temp'] != '' ) {
                update_post_meta( $beer_info_id, 'beer_temp', $_POST['beer_info_temp'] );
            }
            if ( isset( $_POST['beer_info_ibu'] ) && $_POST['beer_info_ibu'] != '' ) {
                update_post_meta( $beer_info_id, 'beer_ibu', $_POST['beer_info_ibu'] );
            }
            if ( isset( $_POST['beer_info_pair'] ) && $_POST['beer_info_pair'] != '' ) {
                update_post_meta( $beer_info_id, 'beer_pair', $_POST['beer_info_pair'] );
            }
    
        }
    }
    
    add_filter( 'template_include', 'include_template_function', 1 );
    
    function include_template_function( $template_path ) {
        if ( get_post_type() == 'nl_beers' ) {
            if ( is_single() ) {
                // checks if the file exists in the theme first,
                // otherwise serve the file from the plugin
                if ( $theme_file = locate_template( array ( 'single-nl_beers.php' ) ) ) {
                    $template_path = $theme_file;
                } else {
                    $template_path = plugin_dir_path( __FILE__ ) . '/single-nl_beers.php';
                }
            }
        }
        return $template_path;
    }
    
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The code is working fine for me. It is updating post meta if we provide proper integer as intval is being used.

    intval( get_post_meta( $beer_info->ID, 'beer_pair', true ) );

    If you will enter alphabet such as ‘a’, it will take the integer value of a variable i.e. ‘0’.

    Hmm…I’ve tried it on two separate installations and it still won’t display the saved input.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Trouble displaying saved custom field value on edit screen’ is closed to new replies.