Update permalinks when new category added to custom post type taxonomy

That’s not how custom taxonomies and post types work

This is what you do:

  • On the init hook, call register_custom_taxonomy or register_post_type
  • If you change your calls to register_custom_taxonomy or register_post_type, or add new ones in your code, regenerate permalinks by visiting the permalinks settings page and clicking save
  • You’re done

So if I follow the above steps and create a new taxonomy named colour, I do not need to flush permalinks to create red or blue terms. If I created a brand new taxonomy named shape I would flush the permalinks, but only once, this is so that I don’t get 404’s when I visit shape terms on the frontend. The same is true of custom post types

Keep in mind that flush_rewrite_rules is a very expensive function, and you should never need to call it. An example of when it might be called is in a plugins activation hook after it’s first installed, or a WP CLI command.

How To Avoid The 404’s

Use register_activation_hook to flush rewrite rules. This will get called when your plugin is activated ( and only the time it’s activated, not every page load ).

https://codex.wordpress.org/Function_Reference/register_activation_hook

Flushing on the creation of every term is both unnecessary, and damaging

What If I Registered My CPT/CT In A Theme?

You can use the after_theme_switch hook to flush rewrite rules, however, registering post types and taxonomies in a theme is bad practice. It causes issues with data portability, e.g. if a user changes themes they loose their data.

Themes determine how a site looks, and functionality, especially data, should be defined in plugins.