0

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.

0

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 . '"> ...';
1
  • I was pretty sure I tried this before, but your code works like a charm. Thank you so much! – BuddyHoli Mar 7 '20 at 10:55

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.