9

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.

2
  • Can you please show the code you are using in register_activation_hook? Commented Jun 15, 2011 at 5:34
  • @Hameedullah Khan code added for clarity on what I've tried so far. Commented Jun 15, 2011 at 11:29

2 Answers 2

10

Here is the fixed version of your code.

class vsetup {
     function __construct() {
          register_activation_hook(__FILE__,array($this,'activate'));
          add_action( 'init', array( $this, 'create_taxonomies' ) );
     } 
     function activate() {
          $this->create_taxonomies();
          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);
     }
}

And don't forget to create the object/instance of the vsetup class i.e new vsetup()

Huh! Forgot to remove the debug statement.

3
  • Oh so the insert didn't work because the taxonomies didn't exist yet. Gonna try this when I get home from work Commented Jun 15, 2011 at 15:31
  • Just to see if I understand what is happening here - register_taxonomy is being called on the "init" hook (so every time) and during the activation hook (which seems to execute before init, otherwise this wouldn't be necessary)? And term insertion is only happening at activation (not init), and even then only after taxonomies have been declared?
    – somatic
    Commented Dec 12, 2011 at 1:19
  • @somatic yes, sir. Commented Dec 12, 2011 at 4:07
1

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:

//assumes some kind of autoloading
$vsetup = new vsetup; //where there might be a file called 'classes/vsetup.class.php'

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:

$vsetup = new vsetup;
register_activation_hook ( __FILE__, array( $vsetup, 'activate' ) );

Maybe this helps someone...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.