WordPress 插件数据消失

发布于 2024-10-12 13:35:54 字数 2992 浏览 3 评论 0原文

我正在开发一个简单的插件,可以让我通过帖子保存成对的值;有一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2024-10-19 13:35:54

问题在于将数据存储为“选项”。只有一个选项表,事实证明,当尝试将数据附加到多个帖子时,它被删除了。

发布元数据是正确的方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文