• Resolved wacoh_327

    (@wacoh_327)


    Hi,

    I’ve been working out with this quite a long time and I can’t find a way how to make it work.

    I’m creating a simple theme option with my theme but putting all the options to a custom database table.
    I want to know how $wpdb->insert() inserts all options like add_option().

    This is my code – https://pastebin.com/5xHBs6r2
    If you look at line 209 function stheme_initialize_theme_options() you’ll see what I mean. I also created a function (function stheme_default_options()) that holds the default options and return it with apply_filters().

    I hope someone can help me with this as I’m really struggling with it for a week now.

    Thank you!
    Best regards.

Viewing 5 replies - 1 through 5 (of 5 total)
  • but putting all the options to a custom database table.

    Why?

    Thread Starter wacoh_327

    (@wacoh_327)

    Yes Esmi, for much cleaner approach when installing and deactivating the theme. people don’t know about the amount of junk left inside db..

    So I’m creating custom table so that when theme deactivates/ uninstall all will be deleted without affecting anything else.

    Thread Starter wacoh_327

    (@wacoh_327)

    nevermind, got it from working. ??

    I was wondering if you could help me? I am trying to insert data into a custom table simply from a form post in the same file.

    Here is my code:

    add_action( 'admin_menu', 'calc_on_admin_menu' );	
    
    public function calc_on_admin_menu() {
    
    		//create new top-level menu
    		add_menu_page( 'Product Calculator', 'Product<br>Calculator', 'manage_options' , 'product-measurement-calculator', array( $this, 'calc_page_setup' ), plugins_url( 'js_product_calculator/admin/img/calculator.png' ) );			
    
    	}
    
    function calc_page_setup() {
    		global $wpdb;
    
    		if (isset($_POST['submit']) ) {
    			$table_name = $wpdb->prefix . 'WH_settings';
    			echo 'testing';
    
    			for($i=0;$i<3; $i++){
    			   $wpdb->insert(
    				   $table_name,
    				   array(
    					   'prod_cat' => 'banners',
    					   'sqft_from' => $_POST['bannerFrom' . [$i]],
    					   'sqft_to' => $_POST['bannerTo' . [$i]],
    					   'm_price' => $_POST['bannerPrice' . [$i]],
    				   )
    			   );
    			}
    		}
    		else {
    		?>	
    
    	<div id="settings_wrap">
              <div id="settings_main">
                 <h1>Product Calculator Settings</h1><hr/>
                 <h3>You can edit settings for specific product categories to work with the Product Calculator</h3>
              </div>
              <form action="" method="post">
                 <input type="submit" value="Save" id="top-submit" />
                     <div class="setting-section">
                         <h2>Banners | <input type="button" value="Add Pricing" /></h2>
                         <table>
                              <tr>
                                    <th colspan="2">Measurement Range (sq. ft.)</th>
                                    <th>Price for Range ($ 0.00)</th>
                               </tr>
                               <?php
                               	$DBresults = $wpdb->get_results('Select * from ' . $wpdb->prefix . 'WH_settings');
                               	$result_count = count($DBresults);
                               	for ($i = 0; $i < $result_count; $i++){
                               ?>
                              <tr><td><input type="text" name="<?php echo 'bannerFrom'.$i; ?>" value="<?php echo $DBresults->sqft_from[$i]; ?>"; /></td><td> - <input type="text" name="<?php echo 'bannerTo'.$i; ?>" value="<?php echo $DBresults->sqft_to[$i]; ?>" /></td><td><input type="text" name="<?php echo 'bannerPrice'.$i; ?>" value="<?php echo $DBresults->m_price[$i]; ?>" /></td></tr>
                              <?php } ?>
                         </table>
                     </div>
    		<input type="submit" name="submit" value="Save" id="top-submit">
              </form>
            </div>
    	<?php }
        }

    I have tried different methods of wpdb->insert with and without the formatting, changing the form action to “options.php” and without but the data is never inserted into the table.

    Do I need to call the calc_page_setup() function on POST?

    thanks

    Nevermind, I figured it out. Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘WordPress Theme Options insert in custom database table with $wpdb->insert()’ is closed to new replies.