Title: plugin development. fields save and update
Last modified: September 1, 2016

---

# plugin development. fields save and update

 *  [sakarya](https://wordpress.org/support/users/sakarya/)
 * (@sakarya)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/plugin-development-fields-save-and-update/)
 * I’m trying to build a simple woocommerce plugin.
 * I want to add new badge for products. There is a simple text form and a checkbox
   for settings.
 * Text form for products published in the last x days.
 * Checkbox for active / deactive
 * Checkbox working fine, but I don’t use text form value. I saw it and saving default
   value. But I can’t upload and use it. When I want to use the field getting undefined
   variable error.
 *     ```
       <?php
   
       /**
        * Custom Woocommerce Options
        *
        * @since  1.0.0
        * @return void
        */
   
       if ( ! defined( 'ABSPATH' ) ) {
       exit; // Exit if accessed directly.
       }
   
       /**
        * Show Woocommerce Customizer Page
        */
       function add_customwoo_admin_menu() {
       add_submenu_page(
           'woocommerce',
           __( 'Custom Woocommerce Options', 'hazirwoo' ),
           __( 'Custom Options', 'hazirwoo' ),
           'manage_woocommerce',
           'hw-woo-custom-options',
           'hw_woocommerce_custom_options_page'
       );
       }
   
       add_action( 'admin_menu', 'add_customwoo_admin_menu' );
   
       /**
        * Customizer Page Content
        */
       function hw_woocommerce_custom_options_page() {
       ?>
       <div class="wrap">
           <h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
           <?php settings_errors(); ?>
   
           <form method="post" action="options.php">
               <?php
   
                   $options = get_option( 'hw_woocommerce_custom_options' );
   
                   $hw_new_badge = $options['hw_new_badge'];
                   $hw_new_day = $options['hw_new_day'];
   
                   settings_fields( 'hw_woocommerce_custom_options_group' );
                   do_settings_sections( 'hw_woocommerce_custom_options_group' );
   
               ?>
   
               <h3><?php _e( 'Custom Functions', 'hazirwoo' ); ?></h3>
   
               <table class="form-table">
                   <tbody>
                       <tr>
                           <th scope="row">Ürün Ayarları</th>
                           <td>
                               <fieldset>
                                   <label for="woocommerce_custom_options_new_badge">
                                   <input id="woocommerce_custom_options_new_badge" name="hw_woocommerce_custom_options[hw_new_badge]" type="checkbox" value="1" <?php checked( $hw_new_badge, 1 ); ?> />
                                   Yeni Ürünleri Belirt</label>
                                   <br>
                                   <div id="newbadge">
                                       <label for="hw_custom_options_new">Yeni Etiketini</label>
                                       <input id="hw_custom_options_new" name="hw_custom_options[hw_new_day]" type="text" value="<?php if(!empty($hw_new_day)) esc_attr_e($hw_new_day, 'hw_woocommerce_custom_options');?>"> <span class="description">güncen önce eklenmiş ürünlerde göster</span>
                                   </div>
                               </fieldset>
                           </td>
                       </tr>
                   </tbody>
               </table>            
   
               <?php submit_button(); ?>
           </form>
       </div>
       <?php
       }
   
       /**
        * Customizer Settings
        */
       function hw_register_functions() {
       register_setting( 'hw_woocommerce_custom_options_group', 'hw_woocommerce_custom_options', 'hw_validate_functions' );
       }
   
       add_action( 'admin_init', 'hw_register_functions' );
   
       /**
        * Validate Settings
        */
       function hw_validate_functions( $input ) {
   
       $valid = array();
   
       $valid['hw_new_badge'] = (isset($input['hw_new_badge']) && !empty($input['hw_new_badge'])) ? 1 : 0 ;
       $valid['hw_new_day'] = (isset($input['hw_new_day']) && !empty($input['hw_new_day'])) ? sanitize_text_field($input['hw_new_day']) : 30;
   
       return $valid;
       }
   
       /**
        * Run
        */
       function hw_custom_woocommerce_options() {
   
       $hw_woo_options = get_option('hw_woocommerce_custom_options');
   
       //var_dump($hw_woo_options);
   
       $day = $hw_woo_options['hw_new_day'];
   
       if($hw_woo_options['hw_new_badge']) {
   
           function hw_show_new_badge() {
               $postdate       = get_the_time( 'Y-m-d' );
               $postdatestamp  = strtotime( $postdate );
   
               if ( ( time() - ( 60 * 60 * 24 * $day ) ) < $postdatestamp ) { // If the product was published within the newness time frame display the new badge
                   echo '<span class="wc-new-badge">' . __( 'YENİ', 'hazirwoo' ) . '</span>';
               }
           }
           add_action( 'woocommerce_before_shop_loop_item_title', 'hw_show_new_badge', 30 ); 
   
       }
       }
       ```
   
 * wp-options table hw_woocommerce_custom_options values;
 * a:12:{ s:12:”hw_new_badge”;i:1; s:10:”hw_new_day”;i:30; }
 * [https://wordpress.org/plugins/woocommerce/](https://wordpress.org/plugins/woocommerce/)

Viewing 1 replies (of 1 total)

 *  [Caleb Burks](https://wordpress.org/support/users/icaleb/)
 * (@icaleb)
 * Automattic Happiness Engineer
 * [9 years, 9 months ago](https://wordpress.org/support/topic/plugin-development-fields-save-and-update/#post-7664325)
 * Would be better to add this to the WooCommerce settings tab (no need for a new
   sub menu just for this). Perhaps add it to WooCommerce > Settings > Products 
   > Display?
 * Then you can use the settings API for this: [https://www.skyverge.com/blog/add-custom-options-to-woocommerce-settings/](https://www.skyverge.com/blog/add-custom-options-to-woocommerce-settings/)
 * To answer your question though, you have this:
 *     ```
       <input id="hw_custom_options_new" name="hw_custom_options[hw_new_day]" type="text"
       ```
   
 * And need to use the correct array name like this:
 *     ```
       <input id="hw_custom_options_new" name="hw_woocommerce_custom_options[hw_new_day]"
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘plugin development. fields save and update’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [plugin-development](https://wordpress.org/support/topic-tag/plugin-development/)

 * 1 reply
 * 2 participants
 * Last reply from: [Caleb Burks](https://wordpress.org/support/users/icaleb/)
 * Last activity: [9 years, 9 months ago](https://wordpress.org/support/topic/plugin-development-fields-save-and-update/#post-7664325)
 * Status: not resolved