The taxonomy is registered as normal through the plugin. For any info regarding this, you can go and check out. register_taxonomy
The part that I needed to highlight and what is relevant to this question, is how to insert new terms.
To insert terms through are quick and easy, wp_insert_terms
but this code can also hurt loading time if not used correctly. The idea is to run that function once, and then never again, almost like removing it after the first run.
To accomplish this, you are going to hook your function to register_activation_hook
This hook runs once, that is when the plugin is activated, it will not rerun on page refresh. The only time it will fire again is when the plugin is deactivated and activated again
So you first need to register your taxonomy as you canβt assign terms to a taxonomy that does not exist. Once your taxonomy is registered, you can insert your terms. Remember, this action will only take place once, and that is when the plugin is activated. If you need to add terms, you need to deactivate the plugin and activate it again
You want to also first check if a term exists before trying to create and insert it.
Here is the plugin code
|
|
Custom taxonomy names (and custom post type names) need to comply to a very specific set of rules, otherwise, you will encounter pitfalls that there is no workaround for.
Here is a guideline when choosing names for custom taxonomies (and custom post types)
- The following are not allowed in custom taxonomy names and custom post type names
- Capital letters or camelcase
- Any type of special character, except underscores (_)
- Spaces
- More than 32 characters for taxonomies and 20 for post types
- Any reserved names, and please note, this goes for any custom naming convention, not just taxonomy names.
- If there is more than one word in a taxonomy name, they have to be separated by underscores, not hyphens (-). I have been challenged that hyphens are the way to go for SEO in urls for taxonomy names. It is true, that is why there are rewrite rules to adjust your URL accordingly. Never change taxonomy names or post type names for URL SEO purposes
Also, you should remove those weird capabilities. It might also create a problem
If this does not solve your issue, please add the code that you use with wp_insert_term
Reference: