我正在使用wp\\u insert\\u term()从csv文件中插入大量分类术语。该csv文件的内容可能会偶尔更改。根据codex,wp\\u insert\\u term()需要在add\\u action(\'init\')上运行,但我只想运行一次。如何在插件激活时实现这一点?
我目前正在插件的构造器中执行此操作,该构造器在每次页面加载时都会运行:
add_action(\'init\', array($this, \'add_geo_terms\'));
然后
public function add_geo_terms() {
$path = plugin_dir_path( __FILE__ ).\'assets/uk-counties.csv\';
$row = 1;
if (($handle = fopen($path, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
if ($c == 1) {
error_log( $data[$c] . "<br />\\n");
wp_insert_term($data[$c],\'reseller-regions\');
}
}
}
fclose($handle);
}
}
我正在使用
wordpress plugin boilerplate 所以我不相信这是真的复制品。
register_activation_hook( __FILE__, array( \'WPS_RESELLER\', \'activate\' ) );
public static function activate( $network_wide ) {
if ( function_exists( \'is_multisite\' ) && is_multisite() ) {
if ( $network_wide ) {
// Get all blog ids
$blog_ids = self::get_blog_ids();
foreach ( $blog_ids as $blog_id ) {
switch_to_blog( $blog_id );
self::single_activate();
}
restore_current_blog();
} else {
self::single_activate();
}
} else {
self::single_activate();
}
}
private static function single_activate() {
add_action(\'init\', \'add_partner_terms\'); // i have tried many variations with classname, self etc
add_action(\'init\', \'add_geo_terms\');
}