WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 8 months ago

#18166 closed enhancement (fixed)

Be able to amend the parent dropdown on the edit taxonomy term page

Reported by: leewillis77 Owned by: SergeyBiryukov
Milestone: 3.7 Priority: normal
Severity: normal Version: 3.2.1
Component: Taxonomy Keywords: has-patch
Focuses: Cc:

Description

It would be useful to filter the parent dropdown on the edit taxonomy term page (/wp-admin/edit-tags.php?taxonomy=foo&post_type=bar).

See attached patch (The filter probably needs a better name, but you get the point).

Attachments (3)

filter.diff (1.1 KB) - added by leewillis77 3 years ago.
Example filter
filter2.diff (1.1 KB) - added by leewillis77 3 years ago.
Revised patch, reverses arguments passed to filter, and svn diff from root.
18166-3.diff (1.1 KB) - added by leewillis77 8 months ago.
Updated patch, renames filter to "taxonomy_parent_dropdown_args"

Download all attachments as: .zip

Change History (16)

leewillis773 years ago

Example filter

comment:1 kawauso3 years ago

  • Summary changed from Would be nice to be able to amend the parent dropdown on the edit taxonomy term page to Be able to amend the parent dropdown on the edit taxonomy term page

apply_filters() only returns one value, so you need to pass the entire array in the first argument for it to be returned.

comment:2 follow-up: leewillis773 years ago

Actually - the patch is exactly what I need, I think perhaps the description I used is misleading.

I want to be able to amend the arguments passed to wp_dropdown_categories. In the use case that led to me reporting this I'm amending the depth parameter so that the end result is that the dropdown list only contains top-level items.

So while my use-case is to cause the contents of the drop-down to be amended, I'm not actually looking to filter the output directly ...

comment:3 in reply to: ↑ 2 kawauso3 years ago

Replying to leewillis77:

Actually - the patch is exactly what I need, I think perhaps the description I used is misleading.

I want to be able to amend the arguments passed to wp_dropdown_categories. In the use case that led to me reporting this I'm amending the depth parameter so that the end result is that the dropdown list only contains top-level items.

So while my use-case is to cause the contents of the drop-down to be amended, I'm not actually looking to filter the output directly ...

Slightly bad wording on my part, the issue is that with your patch the only value eventually passed to wp_dropdown_categories() is $taxonomy (after filtering) since the first value passed to apply_filters() is the one being filtered. You need to be passing the entire arguments array as the first parameter. Also please make patches relative to the root directory.

comment:4 leewillis773 years ago

Ah, right OK. The value returned is up to the filter function itself. In my working example I was doing:

function ($a, $b) {

modify_b();
return $b;

}

But you're right - as a matter of style I guess the arguments should be the other way around, so that the first variable passed is the one that is "filtered". Re-worked patch attached.

leewillis773 years ago

Revised patch, reverses arguments passed to filter, and svn diff from root.

comment:5 dd323 years ago

But you're right - as a matter of style I guess the arguments should be the other way around, so that the first variable passed is the one that is "filtered".

It's not just a matter of style, In the event there's nothing hooked to the filter, the first arg would be returned. So if nothing was using the filter in the original patch, $taxonomy would've been passed to wp_dropdown_categories() instead of the array.

comment:6 dd323 years ago

  • Keywords has-patch added

comment:7 leewillis772 years ago

I'm still having to work-around (Bodge) the lack of this filter - is there anything else I can to hasten its inclusion? The patch still applies cleanly to trunk for info.

comment:8 wonderboymusic8 months ago

  • Keywords needs-patch added; has-patch removed

Seems like the filter should be called taxonomy_parent_dropdown instead of taxonomy_dropdown_args which seems to refer to any ol' wp_dropdown_categories

leewillis778 months ago

Updated patch, renames filter to "taxonomy_parent_dropdown_args"

comment:9 leewillis778 months ago

  • Keywords has-patch added; needs-patch removed

comment:10 follow-up: SergeyBiryukov8 months ago

  • Milestone changed from Awaiting Review to 3.7

$taxonomy is passed as a part of the array, so passing it again as a second argument appears to be redundant.

comment:11 in reply to: ↑ 10 ; follow-up: nacin8 months ago

Replying to SergeyBiryukov:

$taxonomy is passed as a part of the array, so passing it again as a second argument appears to be redundant.

Not necessarily — an earlier filter could change the 'taxonomy' key, rendering the context incorrect. The alternative would be to force 'taxonomy' => $taxonomy after the filter.

We can probably clean this up a bit, stylistically — break this out into a few lines of code.

comment:12 in reply to: ↑ 11 DrewAPicture8 months ago

Replying to nacin:

We can probably clean this up a bit, stylistically — break this out into a few lines of code.

Something like this?

<?php
if ( is_taxonomy_hierarchical( $taxonomy ) ) :
$dropdown_args = array(
        'hide_empty'       => 0,
        'hide_if_empty'    => false,
        'taxonomy'         => $taxonomy,
        'name'             => 'parent',
        'orderby'          => 'name',
        'hierarchical'     => true,
        'show_option_none' => __( 'None' )
);
?>
<div class="form-field">
        <label for="parent"><?php _ex('Parent', 'Taxonomy Parent'); ?></label>
        <?php wp_dropdown_categories( apply_filters ( 'taxonomy_parent_dropdown_args', $dropdown_args ), $taxonomy ); ?>

Not sure I follow forcing 'taxonomy' => $taxonomy after the filter.

comment:13 SergeyBiryukov8 months ago

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

In 25123:

Add 'taxonomy_parent_dropdown_args' filter for the parent dropdown on taxonomy term editing screens. props leewillis77. fixes #18166.

Note: See TracTickets for help on using tickets.