Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Can you please show the code you are using in register_activation_hook? –  Hameedullah Khan Jun 15 '11 at 5:34
    
@Hameedullah Khan code added for clarity on what I've tried so far. –  Manny Fleurmond Jun 15 '11 at 11:29
add comment

1 Answer

up vote 5 down vote accepted

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.

share|improve this answer
    
Oh so the insert didn't work because the taxonomies didn't exist yet. Gonna try this when I get home from work –  Manny Fleurmond Jun 15 '11 at 15:31
    
Works like a charm. Thanks. –  Manny Fleurmond Jun 15 '11 at 22:48
    
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 Dec 12 '11 at 1:19
    
@somatic yes, sir. –  Manny Fleurmond Dec 12 '11 at 4:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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