[wp-trac] [WordPress Trac] #41857: Walker_PageDropdown doesn't set correct selected value when using value_field

WordPress Trac noreply at wordpress.org
Wed Sep 13 04:11:37 UTC 2017


#41857: Walker_PageDropdown doesn't set correct selected value when using
value_field
-------------------------------+------------------------------
 Reporter:  it4life            |       Owner:
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  Posts, Post Types  |     Version:  4.8.1
 Severity:  normal             |  Resolution:
 Keywords:                     |     Focuses:
-------------------------------+------------------------------

Comment (by it4life):

 Replying to [comment:1 birgire]:
 >
 > Not all fields have unique values, like {{{ID}}} and {{{post_name}}}.
 >
 > How should non-unique fields be handled?
 >
 > If the first match is selected, then that would depend on the order -
 that might be problematic.
 >
 About unique field, I have idea to pass `selected` and `value_field` as
 callback:
 {{{#!php
 <?php

 class Custom_Walker_PageDropdown extends Walker_PageDropdown {

         /**
          * Starts the element output.
          * @param array|string $args {
          *
          *    @type int|string|callable $selected     Value of the option
 that should be selected.
          *                                            Default 0.
          *                                            Or a callback that
 return true/false.
          *    @type string|callable     $value_field  Post field or a
 callback used to populate
          *                                            the 'value'
 attribute of the option elements.
          *                                            Default 'ID'.
          *
          * @see Walker_PageDropdown
          */
         public function start_el( &$output, $page, $depth = 0, $args =
 array(), $id = 0 ) {
                 $pad = str_repeat(' ', $depth * 3);

                 $value = $page->ID;
                 if ( isset( $args['value_field'] ) ) {
                         $value_field = $args['value_field'];
                         if ( is_callable( $value_field ) ) {
                                 $value = call_user_func( $value_field,
 $page );
                         } elseif ( isset( $page->{$value_field} ) ) {
                                 $value = $page->{$value_field};
                         }
                 }

                 $output .= "\t<option class=\"level-$depth\" value=\"" .
 esc_attr( $value ) . "\"";

                 if ( isset( $args['selected'] ) ) {
                         $selected = $args['selected'];
                         $is_selected = false;
                         if ( is_callable( $selected ) ) {
                                 $is_selected = call_user_func( $selected,
 $page );
                         } else {
                                 $is_selected = ( $value == $selected );
                         }

                         if ( $is_selected ) {
                                 $output .= ' selected="selected"';
                         }
                 }
                 $output .= '>';

                 $title = $page->post_title;
                 if ( '' === $title ) {
                         /* translators: %d: ID of a post */
                         $title = sprintf( __( '#%d (no title)' ),
 $page->ID );
                 }

                 /**
                  * Filters the page title when creating an HTML drop-down
 list of pages.
                  *
                  * @since 3.1.0
                  *
                  * @param string $title Page title.
                  * @param object $page  Page data object.
                  */
                 $title = apply_filters( 'list_pages', $title, $page );

                 $output .= $pad . esc_html( $title );
                 $output .= "</option>\n";
         }
 }
 }}}

 Usage:
 {{{#!php
 <?php
 wp_dropdown_pages( [
     'value_field' => function ( $page ) {
         return $page->ID . ' - ' . $page->post_name;
     },
     'selected' => function ( $page ) {
         return $page->post_name == 'blog';
     },
     'walker' => new Custom_Walker_PageDropdown,
 ] );
 }}}

 What do you think?

--
Ticket URL: <https://core.trac.wordpress.org/ticket/41857#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list