Добавлен WP CPT, добавлены числовые мета-поля. Ошибка в "Новой записи CPT" - PullRequest
0 голосов
/ 25 февраля 2020

Я добавил CPT под названием «wpna-recipe» в мою установку WordPress через плагин. Затем я создал несколько мета-полей, таких как:

<?php
// ADD METABOX FOR RECIPE INFORMATIONS

function recipe_add_meta_box() {
    add_meta_box( 'recipe_meta_box', __( 'Recipe Information', 'wpna' ), 'recipe_build_meta_box', "wpna-recipe", "normal", "high", null);
    }
add_action( 'add_meta_boxes', 'recipe_add_meta_box' );

function recipe_build_meta_box( $post ){
    // make sure the form request comes from WordPress
    wp_nonce_field( basename( __FILE__ ), 'recipe_meta_box_nonce' );

    // get current meta value
    $current_recipe_time_total = 0;
    $current_recipe_time_prep = get_post_meta( $post->ID, '_recipe_time_prep', true );
    $current_recipe_time_cook = get_post_meta( $post->ID, '_recipe_time_cook', true );
    $current_recipe_time_wait = get_post_meta( $post->ID, '_recipe_time_wait', true );
    $current_recipe_time_total = $current_recipe_time_total 
    + get_post_meta( $post->ID, '_recipe_time_prep', true ) 
    + get_post_meta( $post->ID, '_recipe_time_cook', true ) 
    + get_post_meta( $post->ID, '_recipe_time_wait', true );

    ?>

<div class="wpna-section-1-1">
    <h4><?php _e( 'Duration', 'wpna' ) ?></h4>
    <div class="wpna-section-1-4">
    <label for="recipe-time-prep" class="wpna-label"><?php _e( 'Preperation Time (min.)', 'wpna' ) ?></label>
    <p><input name="recipe_time_prep" type="number" class="wpna-field" value="<?php echo $current_recipe_time_prep; ?>" placeholder="<?php _e( 'Preperation Time (min.)', 'wpna' ) ?>"  min="0" max="20160" /></p>
    </div>
    <div class="wpna-section-1-4">
    <label for="recipe-time-cook" class="wpna-label"><?php _e( 'Cooking Time (min.)', 'wpna' ) ?></label>
    <p><input name="recipe_time_cook" type="number" class="wpna-field" value="<?php echo $current_recipe_time_cook; ?>" placeholder="<?php _e( 'Cooking Time (min.)', 'wpna' ) ?>"  min="0" max="20160" /></p>
    </div>
    <div class="wpna-section-1-4">
    <label for="recipe-time-wait" class="wpna-label"><?php _e( 'Waiting Time (min.)', 'wpna' ) ?></label>
    <p><input name="recipe_time_wait" type="number" class="wpna-field" value="<?php echo $current_recipe_time_wait; ?>" placeholder="<?php _e( 'Waiting Time (min.)', 'wpna' ) ?>"  min="0" max="20160" /></p>
    </div>
    <div class="wpna-section-1-4">
    <label for="recipe-time-total" class="wpna-label"><?php _e( 'Total Time (min.)', 'wpna' ) ?></label>
    <p><input name="recipe_time_total" type="number" class="wpna-field" value="<?php echo $current_recipe_time_total; ?>" placeholder="<?php _e( 'Total Time (min.)', 'wpna' ) ?>"  min="0" max="20160" readonly /></p>
    </div>
</div>

    <?php
}
function recipe_save_meta_box_data( $post_id ){
    // verify meta box nonce
    if ( !isset( $_POST['recipe_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['recipe_meta_box_nonce'], basename( __FILE__ ) ) ){
        return;
    }
    // return if autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
        return;
    }
    // Check the user's permissions.
    if ( ! current_user_can( 'edit_post', $post_id ) ){
        return;
    }
    // store custom fields values for recipe_time_prep
    if ( isset( $_REQUEST['recipe_time_prep'] ) ) {
        update_post_meta( $post_id, '_recipe_time_prep', sanitize_text_field( $_POST['recipe_time_prep'] ));
    }
    // store custom fields values for recipe_time_cook
    if ( isset( $_REQUEST['recipe_time_cook'] ) ) {
        update_post_meta( $post_id, '_recipe_time_cook', sanitize_text_field( $_POST['recipe_time_cook'] ));
    }
    // store custom fields values for recipe_time_wait
    if ( isset( $_REQUEST['recipe_time_wait'] ) ) {
        update_post_meta( $post_id, '_recipe_time_wait', sanitize_text_field( $_POST['recipe_time_wait'] ));
    }
    // store custom fields values for recipe_time_total
    if ( isset( $_REQUEST['recipe_time_total'] ) ) {
        update_post_meta( $post_id, '_recipe_time_total', sanitize_text_field( $_POST['recipe_time_total'] ));
    }

    update_post_meta( $post_id, '_diet_proteine_rule', sanitize_text_field( $_POST['proteine_rule'] ) );
}
add_action( 'save_post', 'recipe_save_meta_box_data' ); 
?>

На экране «Новый рецепт» я получаю ошибки прямо перед полями ввода мета-поля:

Предупреждение: не-число c значение, встречающееся в / www/.../includes/add_meta_recipe.php в строке 19 Предупреждение: не числовое значение c, встречающееся в / www/.../includes/add_meta_recipe.php в строке 20 Предупреждение: не числовое значение c, встречающееся в / www/../includes/add_meta_recipe.php в строке 21

Я думаю, это потому, что эти мета-поля "пустые" при входе в новый экран поста.

Итак, вот мой первый вопрос:

Как мне избавиться от этих сообщений об ошибках (например, установив эти входы на ноль, не теряя эффекта загрузки текущие значения на экране редактирования?

Мой второй вопрос влияет на $ current_recipe_time_total.

Сама формула работает, но обновляется только при отправке сообщения. Как я могу показать сумма _recipe_time_prep, _recipe_time_cook и _recipe_time_wait в реальном времени при вводе чего-либо в поля?

Большое спасибо :) Приятель

...