问题描述
我正在尝试向 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 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。