php - 2nd function not working whan activate the plugin -
hello creating plugin when activate plugin create attribute size , colors in first code creating
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 $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' ); } }
it working 100% fine after creating terms code is
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 $term ) { //if( !term_exists( $term['label'], 'pa_size' ) ){ wp_insert_term( $term['label'], 'pa_size', array( 'slug' => $term['slug'] ) ); //} }
but on first activation plugin adding attribute size , color , term function not working after when reactivate plugin again in 2nd time terms added why not togather working on 1st activation?
when try add data on plugin activate, use function register_activation_hook() in main plugin file.
now try in code
<?php /* plugin name: plugin name plugin uri: http://plugin uri description: plugin description author: 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 $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' ); } } // 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 $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'); ?>
note: best practice write code @ top of file, not neccessry
hope you
Comments
Post a Comment