第二个功能无法激活插件

时间:2014-12-02 09:43:16

标签: 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' => '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'] ) );
    //}
}

但是在第一次激活时,插件只是添加属性大小和颜色以及术语功能在我再次重新激活插件后不再工作第二次增加了为什么它不能在第一次激活时工作?

1 个答案:

答案 0 :(得分:1)

当您尝试在插件激活时添加数据时,请始终在主插件文件中使用此函数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');
?>

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

希望这会对你有所帮助