[英]2nd function not working whan activate the plugin
您好,我在激活插件时正在创建一个插件,它将在我创建的第一个代码中创建属性大小和颜色
global $wpdb;
// attributes parameters
$wpm_attributes = array(
array('label' => 'Size', 'name' => 'size','type' => 'select',),
array('label' => 'Color', 'name' => 'color','type' => 'select',)
);
//create default attributes
foreach ( $wpm_attributes as $attr ) {
$attribute = array(
'attribute_label' => $attr['label'],
'attribute_name' => $attr['name'],
'attribute_type' => $attr['type'],
'attribute_orderby' => 'menu_order'
);
if( !term_exists( $attribute ) ){
$wpdb->insert( $wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute );
delete_transient( 'wc_attribute_taxonomies' );
}
}
我创建术语代码后工作正常100%
global $wpdb;
$size_terms = array(
array('label' => '2-XL', 'slug' => '2-xl'),
array('label' => '3-XL', 'slug' => '3-xl'),
array('label' => '4-XL', 'slug' => '4-xl'),
array('label' => '5-XL', 'slug' => '5-xl'),
array('label' => '6-XL', 'slug' => '6-xl'),
array('label' => 'L', 'slug' => 'l'),
array('label' => 'M', 'slug' => 'm'),
array('label' => 'S', 'slug' => 's'),
array('label' => 'XL', 'slug' => 'xl'),
array('label' => 'XS', 'slug' => 'xs'),
array('label' => 'XXL', 'slug' => 'xxl'),
array('label' => 'Custom Size','slug' => 'custom-size')
);
// //insert default trems
foreach ( $size_terms as $term ) {
//if( !term_exists( $term['label'], 'pa_size' ) ){
wp_insert_term( $term['label'], 'pa_size', array( 'slug' => $term['slug'] ) );
//}
}
但是在第一次激活时,该插件仅添加属性大小和颜色以及术语功能,在此之后,当我再次重新激活该插件时,第二次添加了为什么它不能在第一次激活时一起工作?
当您尝试在插件激活时添加数据时,请始终在主插件文件中使用此函数register_activation_hook() 。
现在在您的代码中尝试
<?php
/*
Plugin Name: Your Plugin Name
Plugin URI: http://Plugin URI
Description: Plugin Description
Author: You
Version: 1.0
Author URI: http://
*/
function function_name(){
global $wpdb;
// attributes parameters
$wpm_attributes = array(
array('label' => 'Size', 'name' => 'size','type' => 'select',),
array('label' => 'Color', 'name' => 'color','type' => 'select',)
);
//create default attributes
foreach ( $wpm_attributes as $attr ) {
$attribute = array(
'attribute_label' => $attr['label'],
'attribute_name' => $attr['name'],
'attribute_type' => $attr['type'],
'attribute_orderby' => 'menu_order'
);
if( !term_exists( $attribute ) ){
$wpdb->insert( $wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute );
delete_transient( 'wc_attribute_taxonomies' );
}
}
// Your Second Data Entry
$size_terms = array(
array('label' => '2-XL', 'slug' => '2-xl'),
array('label' => '3-XL', 'slug' => '3-xl'),
array('label' => '4-XL', 'slug' => '4-xl'),
array('label' => '5-XL', 'slug' => '5-xl'),
array('label' => '6-XL', 'slug' => '6-xl'),
array('label' => 'L', 'slug' => 'l'),
array('label' => 'M', 'slug' => 'm'),
array('label' => 'S', 'slug' => 's'),
array('label' => 'XL', 'slug' => 'xl'),
array('label' => 'XS', 'slug' => 'xs'),
array('label' => 'XXL', 'slug' => 'xxl'),
array('label' => 'Custom Size','slug' => 'custom-size')
);
// //insert default trems
foreach ( $size_terms as $term ) {
//if( !term_exists( $term['label'], 'pa_size' ) ){
wp_insert_term( $term['label'], 'pa_size', array( 'slug' => $term['slug'] ) );
//}
}
}
register_activation_hook(__FILE__, 'function_name');
?>
注意:最佳做法是将此代码写在文件顶部,但不是必需的
希望这个能对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.