Web Design
Mobile Internet
Brand Design
Innovative
News
Encyclopedias

WordPress插件制作教程:將數(shù)據(jù)保存到數(shù)據(jù)庫(kù)

Date:2012-05-06 Aource:尚品中國(guó) Type:網(wǎng)站百科
Word Size:small  medium  big

北京網(wǎng)站建設(shè)公司尚品中國(guó)(m.proteomeinstitute.com):這一篇為大家講解如何將數(shù)據(jù)保存到數(shù)據(jù)庫(kù)中,并且顯示在頁面上,不會(huì)因提交表單時(shí)刷新頁面輸入框中內(nèi)容消失。要實(shí)現(xiàn)這一功能我們需要借助WordPress函數(shù)來實(shí)現(xiàn),下面就來講解具體的實(shí)現(xiàn)方法,先把代碼貼出來。還是根據(jù)之前的那個(gè)插件樣例,大家可以直接在上面做修改。

<?php

//在WordPress后臺(tái)評(píng)論處顯示一個(gè)子菜單出來

add_action('admin_menu', 'my_plugin_menu2');

function my_plugin_menu2() {

add_comments_page('數(shù)據(jù)保存', '數(shù)據(jù)保存', 'read', 'my-unique-identifier','my_plugin_function2');

}

function my_plugin_function2()

{

if($_POST['test_hidden'] == 'y')

{

update_option('test_input_c',$_POST['test_input']);

//更新你添加的數(shù)據(jù)庫(kù)

?>

<div id="message" class="updated">保存成功!</div>

<?php

}

?>

<div class="wrap">

<?php screen_icon(); //顯示圖標(biāo) ?>

<h2>添加數(shù)據(jù)</h2>

<p>在這里進(jìn)行數(shù)據(jù)添加。</p>

<form action="" method="post" id="my_plugin_test_form">

<h3>

<label for="test_input">測(cè)試數(shù)據(jù):</label>

<input type="text" id="test_input" name="test_input" value="<?php echo esc_attr(get_option('test_input_c')); ?>" />

<h3>

<p>

<input type="submit" name="submit" value="保存" />

<input type="hidden" name="test_hidden" value="y" />

</p>

</form>

</div>

<?php

}

//通過get_option()來顯示存在數(shù)據(jù)庫(kù)中的信息。

//以上填寫的信息都存在了數(shù)據(jù)庫(kù)中的wp_options表里面。

?>

上面簡(jiǎn)短的代碼就實(shí)現(xiàn)了數(shù)據(jù)保存并顯示的功能,通過這個(gè)功能我們就可以延伸到其他的方法或者功能上面。比如一些不錯(cuò)的主題都帶有主題設(shè)置,里面的一些顯示和存儲(chǔ)都可以用上面的方法來實(shí)現(xiàn),大家可以多多去嘗試下。大概效果就如下面的樣例圖。

WordPress插件制作教程:將數(shù)據(jù)保存到數(shù)據(jù)庫(kù)

好了這篇文章就寫到這里了,如果有不明白或者好的建議的多多提問和指導(dǎo)哦。
本文發(fā)布于北京網(wǎng)站建設(shè)公司尚品中國(guó)http://m.proteomeinstitute.com/
?

Please contact our consultant

+86 10-60259772

Please provide your contact number. The project manager of shangpin China will contact you as soon as possible.