Help Writing Input Data to Database With Wordpress Plugin
Posted
by HollerTrain
on Stack Overflow
See other posts from Stack Overflow
or by HollerTrain
Published on 2010-05-13T19:41:34Z
Indexed on
2010/05/13
19:44 UTC
Read the original article
Hit count: 237
wordpress-plugin
|Wordpress
Hi I am making a wordpress plugin where I need the Admin to enter data into a database table. I am able to install the db table when the Plugin is activated, however I can't figure out how to save the user input. I've asked on the WP forums but they're dead... Any experienced guru who can lend some guidance would be greatly appreciated.
<?php
/*******************************************************************
* INSTALL DB TABLE - ONLY AT RUN TIME *
*******************************************************************/
function ed_xml_install() {
global $wpdb;
$ed_xml_data = $wpdb->prefix . "ed_xml_data";
if($wpdb->get_var("SHOW TABLES LIKE '$ed_xml_data'") != $ed_xml_data) {
$sql = "CREATE TABLE " . ed_xml_data . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name tinytext NOT NULL,
address text NOT NULL,
url VARCHAR(55) NOT NULL,
phone bigint(11) DEFAULT '0' NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
$name = "Example Business Name";
$address = "1234 Example Street";
$url = "http://www.google.com";
$phone = "523-3232-323232";
$insert = "INSERT INTO " . ed_xml_data .
" (phone, name, address, url) " .
"VALUES ('" . phone() . "','" . $wpdb->escape($name) . "','" . $wpdb->escape($address) . "', '" . $wpdb->escape($url) . "')";
$results = $wpdb->query( $insert );
}
}
//call the install hook
register_activation_hook(__FILE__,'ed_xml_install');
/*******************************************************************
* CREATE MENU, CREATE MENU CONTENT *
*******************************************************************/
if ( is_admin() ){
/* place it under the ED menu */
//TODO $allowed_group = '';
/* Call the html code */
add_action('admin_menu', 'ed_xmlcreator_admin_menu');
function ed_xmlcreator_admin_menu() {
add_options_page('ED XML Creator', 'ED XML Creator', 'administrator',
'ed_xml_creator', 'ed_xmlcreator_html_page');
}
}
/*******************************************************************
* CONTENT OF MENU CONTENT *
*******************************************************************/
function ed_xmlcreator_html_page() {
<div>
<h2>Editors Deal XML Options</h2>
<p>Fill in the below information which will get passed to the .XML file.</p>
<p>[<a href="" title="view XML file">view XML file</a>]</p>
<form method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>
<table width="510">
<!-- title -->
<tr valign="top">
<th width="92" scope="row">Deal URL</th>
<td width="406">
<input name="url" type="text" id="url"
value="<?php echo get_option('url'); ?>" />
</td>
</tr>
<!-- description -->
<tr valign="top">
<th width="92" scope="row">Deal Address</th>
<td width="406">
<input name="address" type="text" id="address"
value="<?php echo get_option('address'); ?>" />
</td>
</tr>
<!-- business name -->
<tr valign="top">
<th width="92" scope="row">Business Phone</th>
<td width="406">
<input name="phone" type="text" id="phone"
value="<?php echo get_option('phone'); ?>" />
</td>
</tr>
<!-- address -->
<tr valign="top">
<th width="92" scope="row">Business Name</th>
<td width="406">
<input name="name" type="text" id="name"
value="<?php echo get_option('name'); ?>" />
</td>
</tr>
</table>
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="hello_world_data" />
<p>
<input type="submit" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
?>
© Stack Overflow or respective owner