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.
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