johnbillion / extended-cpts

A library which provides extended functionality to WordPress custom post types and taxonomies.
GNU General Public License v2.0
977 stars 96 forks source link

Support for taxonomies with mandatory selection #95

Open greatislander opened 6 years ago

greatislander commented 6 years ago

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:

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.)

johnbillion commented 6 years ago

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.

nreynis commented 5 years ago

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?

tedw commented 2 years ago

@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!

montchr commented 1 year ago

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...)