0

I have set up 2 new post meta fields in order to add functionality to a custom post type. The custom fields generally work as expected, with a couple exceptions. I set up a checkbox (called "restaurant-options") so that if checked, a new tab is added on the front end and content from a text area field is shown (called "specials-textarea").

The issue is when I enter info into the text area, it is saved as a custom field but not saved in the text area itself, which appears blank. So, if a post is updated and that content is not re-entered, it is overwritten to blank.

Here is the code I have for the plugin:

/**
 * Adds a meta box to the post editing screen
 */
function gwrrest_custom_meta() {    
    $post_types = array ( 'supplier' );
    foreach( $post_types as $post_type ) {  
        add_meta_box( 
            'gwrrest_meta',
            __( 'Restaurant Options', 'gwrrest-textdomain' ), 
            'gwrrest_meta_callback', 
            $post_type, 
            'normal', 
            'high' 
        );
    } 
}
add_action( 'add_meta_boxes', 'gwrrest_custom_meta' );

/**
 * Outputs the content of the meta box
 */
function gwrrest_meta_callback( $post ) {   
    wp_nonce_field( basename( __FILE__ ), 'gwrrest_nonce' );
    $gwrrest_stored_meta = get_post_meta( $post->ID );
    ?>

    <?php _e( 'Is this a restaurant? ', 'gwrrest-textdomain' )?>
    </span>
    <div class="gwrrest-row-content">
        <label for="restaurant-options">
        <input type="checkbox" name="restaurant-options" id="restaurant-options" value="yes" <?php if ( isset ( $gwrrest_stored_meta['restaurant-options'] ) ) checked( $gwrrest_stored_meta['restaurant-options'][0], 'yes' ); ?> />
        <?php _e( 'Check to add a specials section to the restaurant pages', 'gwrrest-textdomain' )?>
        </label>
    </div>
    </p>
    <p>
        <label for="specials-textarea" class="prfx-row-title"><?php _e( 'Enter Specials Content Here', 'prfx-textdomain' )?></label>
        <textarea name="specials-textarea" id="specials-textarea"><?php if ( isset ( $prfx_stored_meta['specials-textarea'] ) ) echo $prfx_stored_meta['specials-textarea'][0]; ?></textarea>
    </p>
    <?php
}

/**
 * Saves the custom meta input
 */
function gwrrest_meta_save( $post_id ) {
    // Checks save status
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'gwrrest_nonce' ] ) && wp_verify_nonce( $_POST[ 'gwrrest_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }

    // Checks for input and saves
    if ( isset( $_POST['restaurant-options'] ) && $_POST['restaurant-options'] ) {
        add_post_meta( $post_id, 'restaurant-options', 'yes', true );
    } else {
        delete_post_meta( $post_id, 'restaurant-options' );
    }

    // Checks for input and saves if needed
    if( isset( $_POST[ 'specials-textarea' ] ) ) {
        update_post_meta( $post_id, 'specials-textarea', $_POST[ 'specials-textarea' ] );
    } else {
        delete_post_meta( $post_id, 'specials-textarea' );
    }
}
add_action( 'save_post', 'gwrrest_meta_save' );

I have a feeling the issue is how I am setting the update_post_meta. I would also love to know how to keep the text area populated with the data saved as well. Here is a screenshot of the backend.

0

I Found my mistake. Basically, I had a bad prefix... I changed the function name of the get_post_meta to $gwrrest_stored_meta but then missed it in the echo of the text area content. I had "recycled" my code and accidentally left is as $prfx_stored_meta.

So the line here:

 <textarea name="specials-textarea" id="specials-textarea"><?php if ( isset ( $prfx_stored_meta['specials-textarea'] ) ) echo $prfx_stored_meta['specials-textarea'][0]; ?></textarea>

Needed to be changed to:

<textarea name="specials-textarea" id="specials-textarea"><?php if ( isset ( $gwrrest_stored_meta['specials-textarea'] ) ) echo $gwrrest_stored_meta['specials-textarea'][0]; ?></textarea>

Hope that helps someone else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.