Filling a custom data taxonomy of categories with data in WP Plugin

I am writing a wordpress plugin that allows people in the admin panel to hide / show US-specific content. I have the usual category taxonomy called states, and it lists all the states. Admins can check in which conditions they should appear. Pages and messages will not be displayed in a loop if the user state does not match the selected message states.

Now, my question is: how can I populate the plugin with all state data during installation (or remove it when uninstalling)?

+4
source share
1 answer

That should work. You will need to add the rest of the states and make sure that your taxonomy is actually called "States", but other than that it should be fine:

<?php $foo_states = array( 'Alabama', 'Alaska', 'Arizona', 'Arkansas' ); function foo_install() { global $foo_states; foreach ( (array) $foo_states as $state ) { wp_insert_term($state, 'States'); } } register_activation_hook(__FILE__, 'foo_install') function foo_uninstall() { global $foo_states; foreach ( (array) $foo_states as $state ) { wp_delete_term(get_term_by('name', $state, 'States')->term_id, 'States'); } } register_deactivation_hook(__FILE__, 'foo_uninstall'); ?> 
+4
source

All Articles