Consider a plugin which adds a custom Taxonomy and then fills the taxonomy with a large term hierarchy (tens of hierarchical terms, may become hundreds in specific installations). I understand that register_taxonomy() does not store data in the database, therefore it must be used via the init hook so that the taxonomy is registered on each page load. However, wp_insert_term() does store data in the database.

With that in mind, would it be preferable to run wp_insert_term() from register_activation_hook() after ensuring that register_taxonomy() has run? And then run only register_taxonomy() on the 'init' hook for each page load?

For reference, my specific use case is the GeoAreas WP plugin, which currently has about fifty terms. I am develping the next version of the plugin which will support more diverse geographical areas and should support a few hundreds of terms for large geographical areas such as Europe or the US.


50 is relatively not much, and you should create them on the plugin activation hook.

For more then 100 (just pulled the number out of my ass ;) do your own testing on some slow shared hosting) I would create a settings page and initialize the DB from there. The reason is that users do not expect plugin activation to take long and might think that something is wrong, but there is no way to have a kind of "this might take a minute" message at activation time.

Whatever you are deciding to do, don't do it on the init hook as it is used by front end, do it only on hooks that are related to admin.

| improve this answer | |
  • The terms are used in wp_dropdown_categories() for HTML select lists, so it needs to be available before any HTML is sent out. Therefore I propose to only invoke register_taxonomy() in the 'init' hook, and insert the terms only once: on install or from the initialize page that you suggest. – dotancohen Jan 24 '16 at 13:03
  • @dotancohen, you should never write to the DB when serving front end. – Mark Kaplun Jan 24 '16 at 14:02
  • I'm sorry, I wasn't clear. I would invoke wp_insert_term() (which writes to the DB) only from within register_activation_hook(), but invoke register_taxonomy() (which does not write to the DB) on the 'init' hook. Thank you for clarifying that this is the proper method. – dotancohen Jan 24 '16 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.