C
C
chudomozg2019-07-12 03:35:38
Taxonomy
chudomozg, 2019-07-12 03:35:38

On what event do wp_insert_term?

Good afternoon, dear senior WP developers and in general those who can answer me.
I'm a little confused about the WP hook system. I am writing a plugin for WP, I have a task to create several types of posts and add about 20000 taxonomy terms, it would be logical to do this when installing the plugin via register_activation_hook. But according to the documentation and examples, record types are hung on the init hook.

  1. Does this mean that every time WP is loaded, my plugin will try to create these post types?
  2. On what event to hang wp_insert_term? If I set it to init, will my plugin try to add 20,000 taxonomy terms each time WP is loaded?

Thanks in advance...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nikolaev, 2019-07-12
@chudomozg

1. Registration of custom record types and other goodies occurs on init, because otherwise, they simply will not be in the system (although the entries related to them may be in the database). So, register_custom_post_type and register_taxonomy simply change global variables.
2. But wp_insert_term needs to be called only once. It must be understood that it is different from all these register_taxonomyand other things by the fact that it writes data not to a global variable for the needs of the kernel, but to the database, and, accordingly, repeated calls will lead to data duplication at best. Event? The same init will do, just don't forget to put some kind of flag or a banal check for the existence of taxonomies / terms. Some is_terms_added in wp_options .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question