WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#33026 closed enhancement (fixed)

Add option filter for wp_dropdown_categories()

Reported by: theMikeD Owned by: boonebgorges
Milestone: 4.4 Priority: normal
Severity: normal Version: 4.2.2
Component: Taxonomy Keywords: has-patch
Focuses: administration Cc:
PR Number:

Description

I have a case in which I have a two-level hierarchical taxonomy. The only way to enforce this two-level hierarchy is to filter the parent list to top/parent terms only.

For example, given this

parent term 1
    child term 11
    child term 12
parent term 2
    child term 21

the only terms that should show up in the 'Parent' list would be parent term 1 and parent term 2

On the term edit screen, this is handled by using the taxonomy_parent_dropdown_args and quick_edit_dropdown_pages_args filters to add the depth parameter, which works great for this purpose.

But I can't do this for the list presented when a new term is added in the post-edit screen because a similar filter does not exist in wp_dropdown_categories() (the function that creates the list in the post edit screen). This in turn means that I have to create a new metabox from scratch, with a custom walker and an almost verbatim copy of post_categories_meta_box() function just to add this parameter into the args sent to wp_dropdown_categories() which is an ugly hacky workaround.

So lets be consistent and add the new filter.

Attachments (4)

33026.1.patch (4.1 KB) - added by theMikeD 4 years ago.
Patch for 33026
33026.2.patch (4.0 KB) - added by theMikeD 4 years ago.
33026.3.patch (5.2 KB) - added by theMikeD 4 years ago.
33026.4.patch (2.8 KB) - added by theMikeD 4 years ago.

Download all attachments as: .zip

Change History (23)

@theMikeD
4 years ago

Patch for 33026

@theMikeD
4 years ago

#1 @theMikeD
4 years ago

Patch 2 is updated to use a single argument. Works great in testing.

#2 @theMikeD
4 years ago

  • Keywords has-patch added

#3 @theMikeD
4 years ago

As far a writing a test for this goes, is it enough to test the modification of a single element to prove it works? Or must every option be modified and tested? Feels like I'm talking to myself a bit here :)

#4 @theMikeD
4 years ago

  • Type changed from feature request to enhancement

#5 @boonebgorges
4 years ago

  • Keywords needs-patch added; has-patch removed
  • Milestone changed from Awaiting Review to Future Release

Thanks for the patch, theMikeD. A general filter on the args in wp_dropdown_categories() is probably too general an solution here. Elsewhere in the Dashboard interface, we filter the arguments *before* they're passed to wp_dropdown_categories() (that's what 'taxonomy_parent_dropdown_args' etc do), so for consistency, let's do the same thing here. https://core.trac.wordpress.org/browser/tags/4.2.2/src/wp-admin/includes/meta-boxes.php?marks=504#L488

If it's put there, I'd say a better filter name is 'post_edit_category_parent_dropdown_args'.

#6 @theMikeD
4 years ago

No problem. Hows this?

@theMikeD
4 years ago

#7 @theMikeD
4 years ago

  • Keywords has-patch added; needs-patch removed

#8 follow-up: @boonebgorges
4 years ago

  • Keywords needs-patch added; has-patch removed

Thanks, theMikeD. A couple notes on the documentation:

  • Short description should be in the third-person. "Filters" instead of "Filter".
  • Needs a @since 4.4.0 annotation.
  • Don't document every possible argument accepted by wp_dropdown_categories(). The only values passed to the filter are those in the $parent_dropdown_args array.

#9 in reply to: ↑ 8 @theMikeD
4 years ago

Replying to boonebgorges:

  • Short description should be in the third-person. "Filters" instead of "Filter".

This was copied from the docbloc above. Should I fix it there too?

  • Needs a @since 4.4.0 annotation.

I didn't want to be presumptive :) I'll add this.

  • Don't document every possible argument accepted by wp_dropdown_categories(). The only values passed to the filter are those in the $parent_dropdown_args array.

I'll do this now. Thanks for the help.

#10 @boonebgorges
4 years ago

This was copied from the docbloc above. Should I fix it there too?

No, leave it where it already exists. DrewAPicture, could you please verify the recommended style here? See eg [33231]. We should have better documentation about this in https://make.wordpress.org/core/handbook/best-practices/inline-documentation-standards/php/#4-hooks-actions-and-filters.

@theMikeD
4 years ago

#11 @theMikeD
4 years ago

Patch added. Note that I also changed

 * @param array $parent_dropdown_args {
 *     Optional. Array of arguments to generate a categories drop-down element.

to

 * @param string|array $parent_dropdown_args {
 *     Optional. Array or string of arguments to generate a categories drop-down element.

since it takes an array built in the immediately previous line.

Version 0, edited 4 years ago by theMikeD (next)

#12 @theMikeD
4 years ago

Is it proper for me to change it to has-patch when I submit a new one? Is that the correct flow here?

#13 @boonebgorges
4 years ago

  • Keywords has-patch 4.4-early added; needs-patch removed

This looks good to me. Thanks!

Is it proper for me to change it to has-patch when I submit a new one? Is that the correct flow here?

Yes, that works.

#14 @theMikeD
4 years ago

Any chance this can make it into 4.3.1? 4.4 seems like a long way off :)

#15 @boonebgorges
4 years ago

  • Keywords 4.4-early removed
  • Milestone changed from Future Release to 4.4

Nope, 4.4 it is :)

#16 @boonebgorges
4 years ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from new to closed

In 33682:

Filter the arguments passed to wp_dropdown_categories() in the Categories post edit metabox.

The new 'post_edit_category_parent_dropdown_args' provides parity with other
places in wp-admin where wp_dropdown_categories() args are filtered, such as
'taxonomy_parent_dropdown_args'.

Props theMikeD.
Fixes #33026.

#17 @DrewAPicture
4 years ago

In 33686:

Docs: Fix some minor inline documentation syntax issues in the hook docs for the post_edit_category_parent_dropdown_args filter, introduced in [33682].

See #32246. See #33026.

#18 follow-up: @theMikeD
4 years ago

So once this is tagged as 4.4, it's included by default right? Is there anything else I should be doing?

#19 in reply to: ↑ 18 @boonebgorges
4 years ago

Replying to theMikeD:

So once this is tagged as 4.4, it's included by default right? Is there anything else I should be doing?

The filter was added in [33682], and it'll ship as part of 4.4 in a couple of weeks. Your work here is done :)

Note: See TracTickets for help on using tickets.