Php 激活插件时第二个功能不工作

Php 激活插件时第二个功能不工作,php,wordpress,function,plugins,hook,Php,Wordpress,Function,Plugins,Hook,您好,我正在创建一个插件,当我激活插件时,它会在我创建的第一个代码中创建属性大小和颜色 global $wpdb; // attributes parameters $wpm_attributes = array( array('label' => 'Size', 'name' => 'size','type' => 'select',), array('label' => 'Color', 'name' => 'color','type' =&g

您好,我正在创建一个插件,当我激活插件时,它会在我创建的第一个代码中创建属性大小和颜色

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'] ) );
    //}
}

但是,在第一次激活时,插件只是添加属性大小、颜色和术语功能,之后不起作用。当我再次激活插件时,在第二次激活时,术语添加了为什么在第一次激活时不起作用?

当您尝试在插件激活时添加数据时,请始终在主插件文件中使用此功能

现在在你的代码中试试这个

<?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');
?>

注意:最佳做法是在文件顶部编写此代码,但不是必需的


希望这对您有所帮助

这里有人吗?请帮助我修复您是否尝试此函数或未注册\u激活\u hook()如何添加术语价格?我的意思是产品价格是50美元,当我们在头版选择它时,我们设定价格XL$20,所以它应该是$50+$20=$70如何定义每种尺寸的价格?为什么不给我一些时间呢