WordPress 插件数据消失
我正在开发一个简单的插件,可以让我通过帖子保存成对的值;有一些 JS 来添加或删除配对。一切正常,但随机数据会消失。我不确定这是自动保存的问题还是什么,但我想我已经考虑到了这个问题。
知道为什么我的数据正在保存但随机消失吗?
<?php
function esys_menus() {
if (function_exists('add_meta_box')) {
add_meta_box('esys_box','Energy System Details','esys_meta','energy-systems');
}
}
function esys_meta() {
global $wpdb, $post_ID;
$files = esys_get_files($post_ID);
?>
<table>
<thead>
<tr>
<th width="50%">BPM</th>
<th width="50%">Duration</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
if ($files && $post_ID) {
foreach ((array)$files as $file) { ?>
<tr id="esys-<?php echo $i; ?>">
<td><input type="text" name="esys[<?php echo $i; ?>][bpm]" value="<?php echo $file['bpm']; ?>" /></td>
<td><input type="text" name="esys[<?php echo $i; ?>][duration]" value="<?php echo $file['duration']; ?>" /></td>
<td><a href="#" class="button" onclick="esys_remove(<?php echo $i; ?>); return false;">X</a></td>
</tr>
<?php $i++;
}; }; ?>
<tr id="esys-<?php echo $i; ?>">
<td><input type="text" name="esys[<?php echo $i; ?>][bpm]" /></td>
<td><input type="text" name="esys[<?php echo $i; ?>][duration]" /></td>
<td><a href="#" class="button" onclick="esys_remove(<?php echo $i; ?>); return false;">X</a></td>
</tr>
</tbody>
</table>
<p align="right" style="padding:10px 0 5px; margin:0;"><a href="#" class="button" id="add-esys-file">Add</a></p>
<?php
}
function esys_get_files($ID) {
$data = unserialize(get_option("esys-files"));
return ($ID) ? $data[$ID] : $data;
}
function esys_delete() {
delete_option('esys-files');
}
function esys_submit($post_ID) {
global $wpdb;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return $post_id;
}
$data = esys_get_files($post_ID);
foreach((array)$_POST['esys'] as $file) {
$t[] = $file;
$data[$post_ID] = $t;
}
esys_update_files($data);
}
function esys_update_files($data) {
update_option('esys-files', serialize($data));
}
function esys_admin_head() {
echo '<link type="text/css" rel="stylesheet" media="all" href="'.plugins_url('energy-systems-module/admin.css').'" />'."\n";
echo '<script type="text/javascript" src="'.plugins_url('energy-systems-module/esys.js').'"></script>'."\n";
}
add_action('admin_head', 'esys_admin_head');
add_action('admin_menu', 'esys_menus');
add_action('save_post', 'esys_submit');
?>
I'm working on a simple plug that lets me save pairs of values with a post; there's a bit of JS to add or delete pairings. Everything works just fine, but randomly data will disappear. I'm not sure if it's a problem with autosaving or what, but I'd thought I'd accounted for that.
Any ideas why my data is saving, but disappearing randomly?
<?php
function esys_menus() {
if (function_exists('add_meta_box')) {
add_meta_box('esys_box','Energy System Details','esys_meta','energy-systems');
}
}
function esys_meta() {
global $wpdb, $post_ID;
$files = esys_get_files($post_ID);
?>
<table>
<thead>
<tr>
<th width="50%">BPM</th>
<th width="50%">Duration</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
if ($files && $post_ID) {
foreach ((array)$files as $file) { ?>
<tr id="esys-<?php echo $i; ?>">
<td><input type="text" name="esys[<?php echo $i; ?>][bpm]" value="<?php echo $file['bpm']; ?>" /></td>
<td><input type="text" name="esys[<?php echo $i; ?>][duration]" value="<?php echo $file['duration']; ?>" /></td>
<td><a href="#" class="button" onclick="esys_remove(<?php echo $i; ?>); return false;">X</a></td>
</tr>
<?php $i++;
}; }; ?>
<tr id="esys-<?php echo $i; ?>">
<td><input type="text" name="esys[<?php echo $i; ?>][bpm]" /></td>
<td><input type="text" name="esys[<?php echo $i; ?>][duration]" /></td>
<td><a href="#" class="button" onclick="esys_remove(<?php echo $i; ?>); return false;">X</a></td>
</tr>
</tbody>
</table>
<p align="right" style="padding:10px 0 5px; margin:0;"><a href="#" class="button" id="add-esys-file">Add</a></p>
<?php
}
function esys_get_files($ID) {
$data = unserialize(get_option("esys-files"));
return ($ID) ? $data[$ID] : $data;
}
function esys_delete() {
delete_option('esys-files');
}
function esys_submit($post_ID) {
global $wpdb;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return $post_id;
}
$data = esys_get_files($post_ID);
foreach((array)$_POST['esys'] as $file) {
$t[] = $file;
$data[$post_ID] = $t;
}
esys_update_files($data);
}
function esys_update_files($data) {
update_option('esys-files', serialize($data));
}
function esys_admin_head() {
echo '<link type="text/css" rel="stylesheet" media="all" href="'.plugins_url('energy-systems-module/admin.css').'" />'."\n";
echo '<script type="text/javascript" src="'.plugins_url('energy-systems-module/esys.js').'"></script>'."\n";
}
add_action('admin_head', 'esys_admin_head');
add_action('admin_menu', 'esys_menus');
add_action('save_post', 'esys_submit');
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于将数据存储为“选项”。只有一个选项表,事实证明,当尝试将数据附加到多个帖子时,它被删除了。
发布元数据是正确的方法。
The problem was storing data as "options." There is only one options table, and as it turns out, it was getting erased when trying to attach data to more than one post.
Post meta data is the way to go.