How to restrict users and admin from creating new taxonomy terms?

This check: <?php if ( current_user_can( $taxonomy->cap->edit_terms ) ) : ?> needs to be satisfied to display the “Add New Category” link. You can set the capabilities for your schedule_day_taxonomy taxonomy, when you register it. Example Only users with the edit_schedule_day_taxonomy capability can edit the taxonomy: add_action( ‘init’, ‘schedule_day_tax_func’ ); function schedule_day_tax_func() { register_taxonomy( ‘schedule_day_taxonomy’, … Read more

Inserting Category programmatically

Keep track of the return value of wp_insert_term and use it to build your structure. A successful return value will be an array with a term_id key that you can pass into wp_insert_term‘s $args array as parent. $parent = wp_insert_term(‘Science’, ‘category’); // I’ll leave out `$args` here if (is_wp_error($parent)) { // insert didn’t work! return … Read more

Group child category IDs based on their parent category

The quick take would be something like this: $categories = [ ]; $parent_categories = get_categories( [ ‘parent’ => 0 ] ); foreach ( $parent_categories as $parent_category ) { $id = $parent_category->term_id; $categories[ $id ] = wp_list_pluck( get_categories( [ ‘parent’ => $id ] ), ‘term_id’ ); } The important bit is a parent argument, which limits … Read more

Insert terms for custom taxonomy on plugin activation, or each page load (init hook)

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 … Read more

Insert term when page is published – avoid duplicates after edits

I suggest post status transitions that runs only when your set conditions are met. Example: add_action( ‘transition_post_status’, ‘add_awesome_terms’, 10, 3 ); function add_awesome_terms( $new_status, $old_status, $post ) { // only run when it’s a page, new status is publish and old status isn’t publish if ( $post->post_type == ‘page’ && $new_status == ‘publish’ && $old_status … Read more