开发者

Wordpress plugin data disappearing

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>
       开发者_StackOverflow社区         <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');
?>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜