2010-05-13 3 views
1

안녕하세요 저는 데이터베이스 테이블에 데이터를 입력하기 위해 Admin이 필요한 곳에 워드 프레스 플러그인을 만들고 있습니다. 플러그인이 활성화 될 때 db 테이블을 설치할 수 있지만 사용자 입력을 저장하는 방법을 알 수는 없습니다. 저는 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 ['제출 '=='업데이트 옵션 ') { // db에 SQL 바를 추가 }' – HollerTrain

답변

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 

오히려 워드 프레스 옵션을 수정하지 않고, 당신이 당신의 자신의 테이블에 데이터를 삽입하는 것입니다. 그래서 update_option($opt_name, $opt_value); 함수 호출을 데이터베이스 정보를 삽입하는 자신의 함수 호출로 바꿉니다.

다른 방법이 있다면 처음부터 시작하겠습니다. 좀 더 깊이있는 예제를 보려면 RegLevel이라는 플러그인 용으로 작성한 코드를 자유롭게 찾아보십시오. 데이터베이스에 정보를 삽입 할 수있는 단일 관리 페이지가 있습니다. 코드가 어떻게 작동하는지 확인해보십시오. 아마도 자신의 시스템에서 일부 코드를 다시 사용할 수 있습니다.

+0

TY !!!!!!!!!!! !!!!!!!!! – HollerTrain

+0

문제 없습니다. 네가 모든 것을 해결할 수 있기를 바랍니다. – EAMann

+0

이 플러그인은 2 년 넘게 업데이트되지 않았습니다. –

관련 문제