I’ve been trying various ways of doing this. Basically, I’ve been trying to make it so that a plugin of mine populates the terms of a taxonomy only once during the plugin’s activation. The term populating is done in a function via the wp_insert_terms function. Calling the function straight inside the register_activation_hook doesn’t seem to work and neither does hooking to the init hook using the register_activation_hook. Anyone have any ideas?
Here is a version of my code
//version 1
class vsetup {
function __construct() {
register_activation_hook(__FILE__,array($this,'activate'));
$this->create_taxonomies();
}
function activate() {
wp_insert_term('Action','genre');
wp_insert_term('Adventure','genre');
}
function create_taxonomies() {
$genre_args = array(
'hierarchical' => true,
'labels' => array(
'name'=> _x('Genres', 'taxonomy general name' ),
'singular_name' => _x('Genre', 'taxonomy singular name'),
'search_items' => __('Search Genres'),
'popular_items' => __('Popular Genres'),
'all_items' => __('All Genres'),
'edit_item' => __('Edit Genre'),
'edit_item' => __('Edit Genre'),
'update_item' => __('Update Genre'),
'add_new_item' => __('Add New Genre'),
'new_item_name' => __('New Genre Name'),
'separate_items_with_commas' => __('Seperate Genres with Commas'),
'add_or_remove_items' => __('Add or Remove Genres'),
'choose_from_most_used' => __('Choose from Most Used Genres')
),
'query_var' => true,
'rewrite' => array('slug' =>'genre')
);
register_taxonomy('genre', 'post',$genre_args);
}
}
When that didn’t work, I tried doing this:
//version 2
class vsetup {
function __construct() {
register_activation_hook(__FILE__,array($this,'activate'));
$this->create_taxonomies();
}
function activate() {
add_action('init', array($this,'populate_taxonomies'));
}
function create_taxonomies() {
$genre_args = array(
'hierarchical' => true,
'labels' => array(
'name'=> _x('Genres', 'taxonomy general name' ),
'singular_name' => _x('Genre', 'taxonomy singular name'),
'search_items' => __('Search Genres'),
'popular_items' => __('Popular Genres'),
'all_items' => __('All Genres'),
'edit_item' => __('Edit Genre'),
'edit_item' => __('Edit Genre'),
'update_item' => __('Update Genre'),
'add_new_item' => __('Add New Genre'),
'new_item_name' => __('New Genre Name'),
'separate_items_with_commas' => __('Seperate Genres with Commas'),
'add_or_remove_items' => __('Add or Remove Genres'),
'choose_from_most_used' => __('Choose from Most Used Genres')
),
'query_var' => true,
'rewrite' => array('slug' =>'genre')
);
register_taxonomy('genre', 'post',$genre_args);
}
function populate_taxonomies() {
wp_insert_term('Action','genre');
wp_insert_term('Adventure','genre');
}
}
Neither idea worked for me so far.
Here is the fixed version of your code.
And don’t forget to create the object/instance of the vsetup class i.e
new vsetup()
Huh! Forgot to remove the debug statement.
Awesome! This solved my problem. But then I had another. Many people, like me, keep classes in a folder lower than the plugin file that is instantiating them. So you might have in your plugin file:
Well, this is the way I do it, and the above will work if you remove the register activation hook from the class’s constructor, and put it as part of the method that created the instance of vsetup, like:
Maybe this helps someone…