在插件激活期间插入分类术语?

时间:2011-06-15 作者:Manny Fleurmond

我一直在尝试各种方法。基本上,我一直在尝试这样做,以便我的一个插件在插件激活期间只填充一次分类法的术语。术语填充是通过wp\\u insert\\u terms函数在函数中完成的。直接在register\\u activation\\u钩子内调用函数似乎不起作用,使用register\\u activation\\u钩子钩住init钩子也不起作用。有人有什么想法吗?

这是我的代码版本

//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);
     }
}
当这不起作用时,我试着这样做:

 //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\');
         }
    }
到目前为止,这两个想法对我都不起作用。

2 个回复
最合适的回答,由SO网友:Hameedullah Khan 整理而成

这是您的代码的固定版本。

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);
     }
}
不要忘记创建vsetup类的对象/实例,即new vsetup()

哼!忘记删除调试语句。

SO网友:Jim Maguire

令人惊叹的这解决了我的问题。但后来我有了另一个。许多人,像我一样,将类保存在一个低于实例化它们的插件文件的文件夹中。因此,您的插件文件中可能包含:

//assumes some kind of autoloading
$vsetup = new vsetup; //where there might be a file called \'classes/vsetup.class.php\'
好的,这就是我的方法,如果您从类的构造函数中删除register激活挂钩,并将其作为创建vsetup实例的方法的一部分,则上述方法将起作用,如:

$vsetup = new vsetup;
register_activation_hook ( __FILE__, array( $vsetup, \'activate\' ) );
也许这对某人有帮助。。。

结束

相关推荐