开发者

Populate Custom Category Taxonomy with Data in WP Plugin

I'm writing a wordpress plugi开发者_运维知识库n that allows people in the admin to hide/show content specific to US states. I have a custom category taxonomy called States, and it lists all of the states. Admins can check which states they want the post to appear in. Pages and posts will not show up in the loop if the user's state doesn't match up with the posts' selected states.

Now, my question is, how do I populate the plugin with all of the states' data upon install (or remove it upon uninstall)?


This should work. You'll 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');

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜