WP Metabpx Таксономия Термин Dropdowns только частично сохранение - PullRequest
0 голосов
/ 26 февраля 2020

Это безумие, когда код, который был только что скопирован и вставлен, работает для двух из четырех таксономий, но не для третьей и четвертой.

Может быть, кто-то из вас найдет мою "ошибку мозга" ".

Оригинальный код от: https://gist.github.com/charleslouis/5553859

<?php

/* ADD META BOX */
function recipe_add_metabox() {
    remove_meta_box( 'recipe-details', 'nutrix-recipe', 'normal' );
    add_meta_box( 'recipe-details', __('Rezeptdetails', 'nutrix'), 'recipe_metabox_render', 'nutrix-recipe', 'normal', 'high');
}
add_action('add_meta_boxes', 'recipe_add_metabox');

function recipe_metabox_render($post) {
    $tax_name_category = 'nutrix-category';
    $tax_name_difficulty = 'nutrix-difficulty';
    $tax_name_method = 'nutrix-method';
    $tax_name_temperature = 'nutrix-temperature';
    /* $taxonomy = get_taxonomy($tax_name); */
?>
<div class="nutrix-div-1-1">
    <!-- RENDER RECIPE-CATEGORY -->
    <div class="nutrix-div-1-4" id="<?php echo $tax_name_category; ?>">
    <?php 
    wp_nonce_field( plugin_basename( __FILE__ ), 'types_noncename' );
    $type_IDs = wp_get_object_terms( $post->ID, 'nutrix-category', array('fields' => 'ids') );
    $recipe_category_msg = __('Kategorie auswählen', 'nutrix'); 
    wp_dropdown_categories(array(
        'taxonomy'          => 'nutrix-category',
        'hierarchical'      => true,
        'show_option_none'  => $recipe_category_msg,
        'option_none_value' => '',
        'name'              => 'nutrix-category',
        'id'                => 'nutrix_category',
        'hide_empty'        => 0,
        'orderby'           => 'name',
        'order'             => 'ASC',
        'multiple'          => true,
        'selected'          => $type_IDs[0],
                                )
                        );
    ?>
    </div>
    <!-- RENDER RECIPE-METHOD -->
    <div class="nutrix-div-1-4" id="<?php echo $tax_name_method; ?>">
    <?php 
    wp_nonce_field( plugin_basename( __FILE__ ), 'types_noncename' );
    $type_IDs = wp_get_object_terms( $post->ID, 'nutrix-method', array('fields' => 'ids') );
    $recipe_method_msg = __('Methode auswählen', 'nutrix'); 
    wp_dropdown_categories(array(
        'taxonomy'          => 'nutrix-method',
        'hierarchical'      => true,
        'show_option_none'  => $recipe_method_msg,
        'option_none_value' => '',
        'name'              => 'nutrix-method',
        'id'                => 'nutrix_method',
        'hide_empty'        => 0,
        'orderby'           => 'name',
        'order'             => 'ASC',
        'multiple'          => true,
        'selected'          => $type_IDs[0],
                                )
                        );
    ?>
    </div>
    <!-- RENDER RECIPE-DIFFCULTY -->
    <div class="nutrix-div-1-4" id="<?php echo $tax_name_difficulty; ?>">
    <?php 
    wp_nonce_field( plugin_basename( __FILE__ ), 'types_noncename' );
    $type_IDs = wp_get_object_terms( $post->ID, 'nutrix-difficulty', array('fields' => 'ids') );
    $recipe_difficulty_msg = __('Schwierigkeit auswählen', 'nutrix'); 
    wp_dropdown_categories(array(
        'taxonomy'          => 'nutrix-difficulty',
        'hierarchical'      => true,
        'show_option_none'  => $recipe_difficulty_msg,
        'option_none_value' => '',
        'name'              => 'nutrix-difficulty',
        'id'                => 'nutrix_difficulty',
        'hide_empty'        => 0,
        'orderby'           => 'name',
        'order'             => 'ASC',
        'multiple'          => true,
        'selected'          => $type_IDs[0],
                                )
                        );
    ?>
    </div>
     </div>
    <!-- RENDER RECIPE-TEMPERATURE -->
    <div class="nutrix-div-1-4" id="<?php echo $tax_name_temperature; ?>">
    <?php 
    wp_nonce_field( plugin_basename( __FILE__ ), 'types_noncename' );
    $type_IDs = wp_get_object_terms( $post->ID, 'nutrix-temperature', array('fields' => 'ids') );
    $recipe_temperature_msg = __('Temperatur auswählen', 'nutrix'); 
    wp_dropdown_categories(array(
        'taxonomy'          => 'nutrix-temperature',
        'hierarchical'      => true,
        'show_option_none'  => $recipe_temperature_msg,
        'option_none_value' => '',
        'name'              => 'nutrix-temperature',
        'id'                => 'nutrix_temperature',
        'hide_empty'        => 0,
        'orderby'           => 'name',
        'order'             => 'ASC',
        'multiple'          => true,
        'selected'          => $type_IDs[0],
                                )
                        );
    ?>
    </div>
</div>
<?php
}

/* When the post is saved, saves our custom taxonomy */

function recipe_meta_save( $post_id ) {
  if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || wp_is_post_revision( $post_id ) ) 
      return;
  if ( !wp_verify_nonce( $_POST['types_noncename'], plugin_basename( __FILE__ ) ) )
      return;
  if ( 'nutrix-recipe' == $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_page', $post_id ) )
        return;
  }
  else
  {
    if ( !current_user_can( 'edit_post', $post_id ) )
        return;

  /* AUTHENTIFICATION OK, SAVE RECIPE-CATEGORY */
  $type_ID = $_POST['nutrix-category'];
  $type = ( $type_ID > 0 ) ? get_term( $type_ID, 'nutrix-category' )->slug : NULL;
  wp_set_object_terms(  $post_id , $type, 'nutrix-category' );

  /* AUTHENTIFICATION OK, SAVE RECIPE-METHOD */
  $type_ID = $_POST['nutrix-method'];
  $type = ( $type_ID > 0 ) ? get_term( $type_ID, 'nutrix-method' )->slug : NULL;
  wp_set_object_terms(  $post_id , $type, 'nutrix-method' );

  /* AUTHENTIFICATION OK, SAVE RECIPE-DIFFICULTY */
  $type_ID = $_POST['nutrix-difficulty'];
  $type = ( $type_ID > 0 ) ? get_term( $type_ID, 'nutrix-difficulty' )->slug : NULL;
  wp_set_object_terms(  $post_id , $type, 'nutrix-difficulty' );

  /* AUTHENTIFICATION OK, SAVE RECIPE-TEMPERAURE */
  $type_ID = $_POST['nutrix-temperature'];
  $type = ( $type_ID > 0 ) ? get_term( $type_ID, 'nutrix-temperature' )->slug : NULL;
  wp_set_object_terms(  $post_id , $type, 'nutrix-temperature' );
}
}
/* Do something with the data entered */

add_action( 'save_post', 'recipe_meta_save' );


?>

Существуют четыре таксономии. Раскрывающиеся списки отображаются после редактирования для всех четырех. Но в сообщении update / publi sh сохраняются только первые два.

Я пытался найти ошибку в последнем / * AUTHENTIFICATION OK, SAVE RECIPE-XXX * / Lines, но единственная повторяющаяся вещь является типом / tpye_ID и заполняется действием bevor.

Надеюсь, кто-то из вас может дать мне подсказку.

Дополнительная задача: я бы хотел разрешить добавлять "новые термины" через падать. Может ли выбранный вход отображаться как select2?

...