2010-05-13 112 views
1

嗨,我正在制作一个WordPress插件,我需要管理员将数据输入到数据库表中。我可以在插件激活时安装数据库表,但我无法弄清楚如何保存用户输入。我问过WP论坛,但他们已经死了......任何经验丰富的大师谁可以借给一些指导将不胜感激。帮助用Wordpress插件将输入数据写入数据库

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

?> 
+0

会是像 '如果($ _ POST [' 提交 '] == '更新选项'){// SQL添加到瓦尔DB }' – HollerTrain 2010-05-13 21:13:15

回答

3

首先,您需要管理页面上的处理程序来处理已发布的信息。该处理程序将是实际更新数据库表的代码段。

Sample Menu Page in the WP codex有一个如何检查提交的POST数据的例子。这里是一个剪接工作:

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

而不是更新WordPress选项,你插入数据到你自己的表。所以我会用一个调用插入数据库信息的函数替换update_option($opt_name, $opt_value);函数调用。

如果没有别的,这会给你一个开始。有关更深入的示例,请随时浏览我为一个名为RegLevel的插件构建的代码。它有一个管理页面,允许您将信息插入数据库。看看代码,看看它是如何工作的,也许你可以在你自己的系统中重新使用它。

+0

TY !!!!!!!!!!! !!!!!!!!! – HollerTrain 2010-05-13 21:45:51

+0

没问题。我希望你能把所有的东西都弄清楚。 – EAMann 2010-05-13 21:59:09

+0

该插件在2年内未更新。 – 2015-02-09 22:35:09