[wp-trac] [WordPress Trac] #43517: Adding support of default category term for custom taxonomies
WordPress Trac
noreply at wordpress.org
Fri Apr 9 19:55:15 UTC 2021
#43517: Adding support of default category term for custom taxonomies
------------------------------------+------------------------
Reporter: enrico.sorcinelli | Owner: whyisjake
Type: defect (bug) | Status: closed
Priority: normal | Milestone: 5.5
Component: Taxonomy | Version:
Severity: normal | Resolution: fixed
Keywords: has-patch has-dev-note | Focuses:
------------------------------------+------------------------
Comment (by johnjamesjacoby):
A bit of feedback about this feature, now that I've implemented it into a
plugin I help maintain.
Coupling the `'default_term'` WP_Taxonomy argument with a registered
setting (for storing the ID of the default term) puts developers into a
catch 22 situation.
The setting stores a term ID, but the argument requires a term Name. 🤔
So you don't want to register the taxonomy without `default_term` (because
you want all the admin-area UI goodies that come with that argument being
non-empty) but you can't get the default-term name from the database
without a call to `get_term()` which works best when it's supplied a
taxonomy, which isn't registered yet. 🔁
It means an early `get_term()` call that may potentially have legacy
issues with shared terms even before the call to `register_taxonomy()`
happens to register the taxonomy being queried for via `get_term()`. 🔁
{{{
// Default arguments
$args = array();
// Default term
$default = get_option( 'default_term_taxonomy_name' );
// Default exists
if ( ! empty( $default ) ) {
// Get term (does not use taxonomy, because it's not registered
yet)
$term = get_term( $default );
// Term exists
if ( ! empty( $term->name ) ) {
$args['default_term'] = array( 'name' => $term->name );
}
}
// Register
register_taxonomy(
'taxonomy_name',
'post_type,
$args
);
}}}
This above code and approach will work reliably for sites that do not have
shared terms, but has a small potential to not work correctly on sites
that are sharing terms across taxonomies.
It also was quite weird to implement. I had hoped the `default_term`
argument would accept the numeric ID of the value straight from the
registered setting, handling verification & fallback internally. I
understand why that isn't how it works, but it also doesn't seem super
great to pass that burden down to each plugin that uses it.
I'm glad to see this feature exist and working how it does in WordPress
admin, and hope to see further improvements to it. 🙏
--
Ticket URL: <https://core.trac.wordpress.org/ticket/43517#comment:43>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list