开发者

Help Writing Input Data to Database With Wordpress Plugin

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 bel开发者_如何学Goow 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>

?>


First things first, you need a handler on your admin page to do something with the information that's been posted back. That handler will be the section of code that actually updates your database table.

The Sample Menu Page in the WP codex has an example of how to check for submitted POST data. Here's a snipped to work with:

// mt_options_page() displays the page content for the Test Options submenu
function mt_options_page() {

    // variables for the field and option names 
    $opt_name = 'mt_favorite_food';
    $hidden_field_name = 'mt_submit_hidden';
    $data_field_name = 'mt_favorite_food';

    // Read in existing option value from database
    $opt_val = get_option( $opt_name );

    // See if the user has posted us some information
    // If they did, this hidden field will be set to 'Y'
    if( isset($_POST[ $hidden_field_name ]) && $_POST[ $hidden_field_name ] == 'Y' ) {
        // Read their posted value
        $opt_val = $_POST[ $data_field_name ];

        // Save the posted value in the database
        update_option( $opt_name, $opt_val );

        // Put an options updated message on the screen

?>
<div class="updated"><p><strong><?php _e('Options saved.', 'mt_trans_domain' ); ?></strong></p></div>
<?php

    }

    // Now display the options editing screen

Rather than updating a WordPress option, you're inserting data into your own table. So I'd replace the update_option( $opt_name, $opt_value); function call with a call to your own function that inserts database information.

If nothing else, this will give you a start. For a more in-depth example, feel free to browse the code I built for a plug-in called RegLevel. It has a single admin page that allows you to insert information into the database. Take a look at the code to see how it works, maybe you can re-purpose some of it in your own system.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜