| 563 | * Display taxonomy assignment dropdown form. |
| 564 | * |
| 565 | * @since 4.7.0 |
| 566 | * |
| 567 | * @param WP_Post $post Post object. |
| 568 | * @param array $box { |
| 569 | * Categories meta box arguments. |
| 570 | * |
| 571 | * @type string $id Meta box 'id' attribute. |
| 572 | * @type string $title Meta box title. |
| 573 | * @type callable $callback Meta box display callback. |
| 574 | * @type array $args { |
| 575 | * Extra meta box arguments. |
| 576 | * |
| 577 | * @type string $taxonomy Taxonomy. Default 'category'. |
| 578 | * } |
| 579 | * } |
| 580 | */ |
| 581 | function taxonomy_dropdown_meta_box( $post, $box ) { |
| 582 | $defaults = array( 'taxonomy' => 'category' ); |
| 583 | |
| 584 | //// @TODO: Meta box title should be singular (probably), |
| 585 | /// currently from edit-form-advanced you get $taxonomy->labels->name. |
| 586 | /// Maybe it should be a new label? |
| 587 | if ( ! isset( $box['args'] ) || ! is_array( $box['args'] ) ) { |
| 588 | $args = array(); |
| 589 | } else { |
| 590 | $args = $box['args']; |
| 591 | } |
| 592 | |
| 593 | $r = wp_parse_args( $args, $defaults ); |
| 594 | $tax_name = esc_attr( $r['taxonomy'] ); |
| 595 | $taxonomy = get_taxonomy( $tax_name ); |
| 596 | |
| 597 | $input_name = ( $tax_name == 'category' ) ? 'post_category[]' : 'tax_input[' . $tax_name . '][]'; |
| 598 | $input_id = "tax-input-{$tax_name}"; |
| 599 | |
| 600 | // Hierarchical taxonomies expect term ID, non-hierarchical the slug |
| 601 | if ( is_taxonomy_hierarchical( $tax_name ) ) { |
| 602 | $hierarchical = 1; |
| 603 | $value_field = 'term_id'; |
| 604 | } else { |
| 605 | $hierarchical = 0; |
| 606 | $value_field = 'slug'; |
| 607 | } |
| 608 | |
| 609 | $terms = get_the_terms( $post, $tax_name ); |
| 610 | |
| 611 | if ( empty( $terms ) ) { |
| 612 | $selected = 0; |
| 613 | } else { |
| 614 | // If there is more than one term assigned, only the first one will be selected |
| 615 | // and thus a post save will result in only that term being assigned. |
| 616 | // This case could happen if changing a taxonomy's assignment callback |
| 617 | // from one that allows multiple select to not. |
| 618 | $selected = array_shift( $terms ); |
| 619 | $selected = $selected->$value_field; |
| 620 | } |
| 621 | ?> |
| 622 | <div id="taxonomy-<?php echo $tax_name; ?>" class="tax-dropdown"> |
| 623 | <label for="tax-input-<?php echo $tax_name; ?>"><?php echo $taxonomy->labels->add_or_remove_items; ?></label> |
| 624 | <?php |
| 625 | $dropdown_args = array( |
| 626 | 'taxonomy' => $tax_name, |
| 627 | 'hide_empty' => 0, |
| 628 | 'id' => $input_id, |
| 629 | 'name' => $input_name, |
| 630 | 'orderby' => 'name', |
| 631 | 'hierarchical' => $hierarchical, |
| 632 | //// @TODO: Categories can't really have a "none" because of default category |
| 633 | 'show_option_none' => __( '— Select —' ), |
| 634 | 'option_none_value' => 0, |
| 635 | 'value_field' => $value_field, |
| 636 | 'selected' => $selected, |
| 637 | ); |
| 638 | |
| 639 | /** |
| 640 | * Filters the arguments for the taxonomy select dropdown on the Post Edit page. |
| 641 | * |
| 642 | * @since 4.7.0 |
| 643 | * |
| 644 | * @param array $dropdown_args { |
| 645 | * Optional. Array of arguments to generate parent dropdown. |
| 646 | * |
| 647 | * @type string $tax_name Name of the taxonomy to retrieve. |
| 648 | * @type bool $hide_if_empty True to skip generating markup if no |
| 649 | * terms are found. Default 0. |
| 650 | * @type string $id Value for the 'id' attribute |
| 651 | * of the select element. |
| 652 | * Default "tax-input-{$tax_name}" where |
| 653 | * `$tax_name` is the taxonomy name. |
| 654 | * @type string $name Value for the 'name' attribute |
| 655 | * of the select element. |
| 656 | * Default "post_category" or |
| 657 | * "tax_input[{$tax_name}] where |
| 658 | * `$tax_name` is the taxonomy name. |
| 659 | * @type string $orderby Which column to use for ordering |
| 660 | * terms. Default 'name'. |
| 661 | * @type bool|int $hierarchical Whether to traverse the taxonomy |
| 662 | * hierarchy. Default 1. |
| 663 | * @type string $show_option_none Text to display for the "none" option. |
| 664 | * Default "— Select —". |
| 665 | * @type string $option_none_value Value for the "none" option. Default 0. |
| 666 | * @type string $selected Value for the selected option. |
| 667 | * Default a term ID or slug. |
| 668 | * } |
| 669 | */ |
| 670 | $dropdown_args = apply_filters( 'post_edit_taxonomy_dropdown_args', $dropdown_args ); |
| 671 | |
| 672 | wp_dropdown_categories( $dropdown_args ); |
| 673 | //// @TODO: Adding new, with a parent for hierarchical, and a filter to disable? |
| 674 | ?> |
| 675 | </div> |
| 676 | <?php |
| 677 | } |
| 678 | |
| 679 | /** |