extended-cpts icon indicating copy to clipboard operation
extended-cpts copied to clipboard

Support for taxonomies with mandatory selection

Open greatislander opened this issue 8 years ago • 4 comments

I'm wondering if you'd be interested in a PR which adds support for registering a custom taxonomy where a term must always be selected (same behaviour as the category taxonomy). I can think of a few approaches to this but my preference would be to add two parameters to the registration function:

  • allow_empty: default true, accepts bool
  • default_term: default null, accepts string (term name)

When allow_empty is set to false, the first term listed will be selected by default and the No <taxonomy name> option will be removed from radio / dropdown taxonomies. If default_term is set to a string, a matching term will created and selected by default (or selected if it exists). Thoughts?

(Note: the default_term could be used for taxonomies which allow no selection as well, so maybe this could be two PRs.)

greatislander avatar Dec 19 '17 20:12 greatislander

Yeah I'd definitely be happy to take a look at a PR for that.

There's some semi-related code in place already which allows for a fallback default term from a stored option if you use a taxonomy term in the permalink structure.

See also #71.

johnbillion avatar Dec 20 '17 13:12 johnbillion

By reading the documentation I was under the assumption that required option was doing just that.

But it seems it is just adding a required attribute (and only in the dropdown widget) that has no effect at all (at least in Gutemberg) because the taxonomy still accepts the empty value. Shouldn't that be the role of the required option?

nreynis avatar Aug 02 '19 09:08 nreynis

@nreynis I was also surprised to find this out about required. Do you have a preferred solution? I’m considering this plugin https://wordpress.org/plugins/required-taxonomies/ Thanks!

tedw avatar May 16 '22 15:05 tedw

I've been hoping for this feature as well, so I investigated the Required Taxonomies plugin mentioned in the previous comment. Unfortunately the author of that plugin has decided to make custom post type support a premium feature... (but it's licensed GPLv2 and the code itself looks pretty minimal aside from the embedded promotional stuff, so it's prob not too hard to adapt...)

astratagem avatar Mar 03 '23 03:03 astratagem