plugins - How to Save settings of custom tab product page in admin side in a database?

admin2025-01-08  4

I have following code :


                    <tr>
                        <th><?php _e('Custom Stock Message'); ?></th>
                             <td>
                                <input type="text" name="customstock-msg"  value=""/>
                             </td>
                    </tr>

                    <tr>
                        <th><?php _e('Order Processing Time'); ?></th>
                             <td>

                               <input type="text" name="customstock-Processing-time">
                             </td>
                    </tr>

                    <tr>
                        <th><?php _e('Instock Date'); ?></th>
                             <td>
                               <!-- <input type="date" name="customstock-date"  value=""/>-->
                               <input type="text" id="datepicker" name="customstock-instockdate">
                             </td>
                    </tr>

                    <tr>
                        <th><?php _e('Show Quantity when Instock'); ?></th>
                             <td>
                               <!-- <input type="date" name="customstock-date"  value=""/>-->
                               <select name="customstock-quantity" id="showquantity">
                                    <option value="yes">Yes</option>
                                    <option value="no"> No</option>
                               </select>
                             </td>
                    </tr>

                    <tr>
                        <th><?php _e('Show on Catlog Page'); ?></th>
                             <td>
                               <!-- <input type="date" name="customstock-date"  value=""/>-->
                               <select name="customstock-catlogpage" id="showcatlogpage">
                                    <option value="yes">Yes</option>
                                    <option value="no"> No</option>
                               </select>
                             </td>
                    </tr>

                    ?>

                     <p>
                     <input type="submit" class="button-primary" name="customstock_submit_specific_product" value="<?php  _e('Save Changes') ?>" />
                     </p>
                  </form>

  if(isset($_POST['customstock_submit_specific_product']))
 {
                    global $wpdb,$product;
                     $id = $product->id;

                    $custommsg = sanitize_text_field( $_POST['customstock-msg'] );
                    $customprocessingtime = sanitize_text_field( $_POST['customstock-Processing-time'] );
                    $customstockquantity = sanitize_text_field( $_POST['customstock-quantity'] );
                    $customstockcatlogpage = sanitize_text_field( $_POST['customstock-catlogpage'] );
                    $customstockinstockdate = sanitize_text_field( $_POST['customstock-instockdate'] );
                    $customstockinstockdate = date("Y-m-d", strtotime($customstockinstockdate) );

  $wpdb->insert('wp_woocommerce_specific_product_settings', array(
                                                                    'custom_msg' => $custommsg,
                                                                    'order_processing_time'  => $customprocessingtime,
                                                                    'exp_instock_date' => $customstockinstockdate, 
                                                                'show_stockstatus_quantity' => $customstockquantity,
                                                                    'showon_catlog' => $customstockcatlogpage,
                                                                    'specific_product_id' =>  $id
                                                                ));
  }

Above code is ececuted when submit button is press. I don't want to store in wp_postmeta then what to do.I am beginner in a wordpress.Anyone have idea how I can save it?

I have following code :


                    <tr>
                        <th><?php _e('Custom Stock Message'); ?></th>
                             <td>
                                <input type="text" name="customstock-msg"  value=""/>
                             </td>
                    </tr>

                    <tr>
                        <th><?php _e('Order Processing Time'); ?></th>
                             <td>

                               <input type="text" name="customstock-Processing-time">
                             </td>
                    </tr>

                    <tr>
                        <th><?php _e('Instock Date'); ?></th>
                             <td>
                               <!-- <input type="date" name="customstock-date"  value=""/>-->
                               <input type="text" id="datepicker" name="customstock-instockdate">
                             </td>
                    </tr>

                    <tr>
                        <th><?php _e('Show Quantity when Instock'); ?></th>
                             <td>
                               <!-- <input type="date" name="customstock-date"  value=""/>-->
                               <select name="customstock-quantity" id="showquantity">
                                    <option value="yes">Yes</option>
                                    <option value="no"> No</option>
                               </select>
                             </td>
                    </tr>

                    <tr>
                        <th><?php _e('Show on Catlog Page'); ?></th>
                             <td>
                               <!-- <input type="date" name="customstock-date"  value=""/>-->
                               <select name="customstock-catlogpage" id="showcatlogpage">
                                    <option value="yes">Yes</option>
                                    <option value="no"> No</option>
                               </select>
                             </td>
                    </tr>

                    ?>

                     <p>
                     <input type="submit" class="button-primary" name="customstock_submit_specific_product" value="<?php  _e('Save Changes') ?>" />
                     </p>
                  </form>

  if(isset($_POST['customstock_submit_specific_product']))
 {
                    global $wpdb,$product;
                     $id = $product->id;

                    $custommsg = sanitize_text_field( $_POST['customstock-msg'] );
                    $customprocessingtime = sanitize_text_field( $_POST['customstock-Processing-time'] );
                    $customstockquantity = sanitize_text_field( $_POST['customstock-quantity'] );
                    $customstockcatlogpage = sanitize_text_field( $_POST['customstock-catlogpage'] );
                    $customstockinstockdate = sanitize_text_field( $_POST['customstock-instockdate'] );
                    $customstockinstockdate = date("Y-m-d", strtotime($customstockinstockdate) );

  $wpdb->insert('wp_woocommerce_specific_product_settings', array(
                                                                    'custom_msg' => $custommsg,
                                                                    'order_processing_time'  => $customprocessingtime,
                                                                    'exp_instock_date' => $customstockinstockdate, 
                                                                'show_stockstatus_quantity' => $customstockquantity,
                                                                    'showon_catlog' => $customstockcatlogpage,
                                                                    'specific_product_id' =>  $id
                                                                ));
  }

Above code is ececuted when submit button is press. I don't want to store in wp_postmeta then what to do.I am beginner in a wordpress.Anyone have idea how I can save it?

Share Improve this question edited Apr 6, 2016 at 10:25 Dipika asked Apr 6, 2016 at 10:14 DipikaDipika 1051 gold badge1 silver badge9 bronze badges 3
  • Why you do not want to save in post meta ? Also please format your code! – Sumit Commented Apr 6, 2016 at 10:19
  • I easily identify store value and retrieve easily so i want to store in another table and retrieve easily. – Dipika Commented Apr 6, 2016 at 10:23
  • The most easiest way is going in WordPress way. If you still want to with custom table then please show your code where you've implemented custom query. – Sumit Commented Apr 6, 2016 at 10:55
Add a comment  | 

1 Answer 1

Reset to default 0

Depending on your situation I don't recommend or condone doing this, but the answer to your question is to take a look at the $wpdb class and the dbDelta() function. This is the interface WordPress provides for adding custom tables.

Be careful though, with great power comes great responsibility. Using $wpdb bypasses many of the built-in WordPress security features, so you need to be very cautious with sanitizing and escaping data to avoid introducing SQL injection vulnerabilities and other security holes.

That said, here's an abstracted class I recently used to do this for a client that may be useful for you to work with. I didn't test this, it's an abstraction from a much larger class so you'll need to re-configure and test it yourself. For what it's worth, I don't think you need a custom table for this use case.

<?php
/**
 * Database
 *
 * Add custom tables to a WordPress database
 */

if ( ! class_exists('DB_Table') ) :

class DB_Table 
{
    protected $table;

    /**
     * Create Database Table
     *
     * Given a schema array, create a custom table in the WP database
     */
    public static function create() {
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

        global $wpdb, $charset_collate;

        $table = 'custom_table_name'; // The name of your custom DB table
        $schema = self::schema();

        // create database table
        $table_name = $wpdb->prefix . $table;
        $sql = "CREATE TABLE IF NOT EXISTS $table_name ( ";
        $sql .= $schema;
        $sql .= " ) $charset_collate;";

        // run create process
        dbDelta( $sql );
    }

    /**
     * Schema: Level Term Rates 
     *
     * Schema definition for the custom table
     */
    public static function schema() {

        // Define your schema here for the table
        $schema = "id int(8) unsigned NOT NULL AUTO_INCREMENT,
        age int(3) NOT NULL DEFAULT '0',
        first_name text NOT NULL DEFAULT '',
        last_name text NOT NULL DEFAULT '',
        gender char(1) NOT NULL DEFAULT '',
        PRIMARY KEY (id)";

        return $schema;
    }
}

/**
 * Register Hooks
 */
register_activation_hook( __FILE__, array( 'DB_Table', 'create' ) );

endif;
?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265924a1080.html

最新回复(0)