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; }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘plugin development. fields save and update’ is closed to new replies.