問題描述
我正在嘗試向 WordPress 小工具動態新增表單域。所以如果使用者想要新增一個事件的日期,他們可以點選一個按鈕獲得更多的欄位。
問題是:如何儲存新建立的輸入欄位到我的資料庫?我需要編寫自定義更新功能嗎?任何提示?
這是小工具的外觀:
這是我的小工具的 PHP 程式碼 (到目前為止):
class Spillelister extends WP_Widget {
public function Spillelister() {
$widget_options = array (
'classname' => 'spillelister-widget',
'description' => 'Widget for å illustrere spillelister.');
parent::WP_Widget('spillelister_widget', 'Spilleplan', $widget_options);
}
// The actual widget user interface
public function widget($args, $instance) {
extract( $args, EXTR_SKIP);
$title = ( $instance['title'] ) ? $instance['title'] : 'Spilleplan';
$body = ( $instance['body'] ) ? $instance['body'] : 'Ingen flere forestillinger';
?>
<?php echo $before_widget; ?>
<?php echo $before_title . $title . $after_title; ?>
<p><?php echo $body; ?></p>
<?php
}
public function update() {
}
public function form() {
?>
<div class="date_stamp">
<p>
<label>Dato</label> <input type="text" class="datepicker">
<br>
<label>Tid</label> <input type="text">
<span class="remove">Remove</span>
</p>
</div>
<p>
<span class="add">Add fields</span>
</p>
<?php
}
}
function spillelister_init() {
register_widget('Spillelister');
}
add_action('widgets_init', 'Spillelister_init');
任何提示,提示或答案都不勝感激。 🙂
最佳解決方案
有趣的問題!我從來沒有看到在 Widget 中使用的可重複的欄位。給一個完整的答案將需要太多的工作/時間,所以我會給你連結到我所知道的資源,希望你會做這個工作,並與我們分享解決方案;
所有這些例子都涉及到 Meta Boxes,您需要複製 jQuery 指令碼,並將 post_meta 修改為 Widgets 的情況。
-
Create more Meta Boxes as needed – WPSE Q& A
-
Repeatable Custom Fields in a Metabox – Gist
/**
* Repeatable Custom Fields in a Metabox
* Author: Helen Hou-Sandi
*
* From a bespoke system, so currently not modular - will fix soon
* Note that this particular metadata is saved as one multidimensional array (serialized)
*/
-
Repeatable Custom Fields in a Metabox – 另一個 Gist 的例子,沒有給出描述。這一個很有趣,因為它具有對欄位進行排序的程式碼。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。
