Question

i have a custom field like this:

function nutrix_display_recipe_mb( $post_object ) {
global $post;
wp_nonce_field( basename( __FILE__ ), 'recipe_mb_nonce' );

// PREPARATION TIME FIELD
    $html = '';  
    $label = __( 'Preparation Time (min): ', 'nutrix' ); 
    $current_preptime = get_post_meta( $post_object->ID, 'nutrix_preparation_time', true );

    $html .= '<div class="nutrix-div-25"><p><label for="nutrix_preparation_time">' . $label . '</label><br /><input type="number" id="nutrix_preparation_time" name="nutrix_preparation_time" value="<?php echo $current_preptime; ?>" min="0" />';

    $html .= '</input></p></div>';

    echo $html;
}

function nutrix_save_recipe_mb( $post_id, $post ) {
// AUTOSAVE CHECK, NONCE AND MORE
if ( $post->post_type == 'nutrix-recipe' ) {
if ( isset( $_REQUEST['nutrix_preparation_time'] ) )
            update_post_meta( $post_id, 'nutrix_preparation_time', sanitize_text_field( $_POST['nutrix_preparation_time'] ) );
        else
            delete_post_meta( $post_id, 'nutrix_preparation_time' );


    }
}

The field is saving when updating the post, I can see the value in phpmyAdmin. But for some reason, when entering a saved post, the saved value is not shown in the input.

Analyzing with my very basic knowledge, this can only be caused by this:

$current_preptime = get_post_meta( $post_object->ID, 'nutrix_preparation_time', true );

or this

<input.... value="<?php echo $current_preptime; ?>" ...>

I didn´t find my mistake. Would really appreciate your help guys.

Was it helpful?

Solution

You are building concatenating parts for your $html value but have <?php echo ...?> there, and event it is never run, as it's inside single quoted string. Fix your parts for input field like this

$html .= '<div> ... <input ... value="' . $current_preptime . '"> ...';
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top