This plugin is used to store pairs of data with a post. Everything gets saves just find, but randomly, data will vanish.
Help! I have no idea why this is happening.
function post_head_menus() {
if (function_exists('add_meta_box')) {
add_meta_box('post_head_box','Energy System Details','post_head_meta','energy-systems');
}
}
function post_head_meta() {
global $wpdb, $post_ID;
$files = post_head_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="post-head-<?php echo $i; ?>">
<td><input type="text" name="post-head[<?php echo $i; ?>][bpm]" value="<?php echo $file['bpm']; ?>" /></td>
<td><input type="text" name="post-head[<?php echo $i; ?>][duration]" value="<?php echo $file['duration']; ?>" /></td>
<td><a href="#" class="button" onclick="post_head_remove(<?php echo $i; ?>); return false;">X</a></td>
</tr>
<?php $i++;
}; }; ?>
<tr id="post-head-<?php echo $i; ?>">
<td><input type="text" name="post-head[<?php echo $i; ?>][bpm]" /></td>
<td><input type="text" name="post-head[<?php echo $i; ?>][duration]" /></td>
<td><a href="#" class="button" onclick="post_head_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-post-head-file">Add</a></p>
<?php
}
function post_head_get_files($ID) {
$data = unserialize(get_option("post-head-files"));
return $data[$ID];
}
function post_head_delete() {
delete_option('post-head-files');
}
function post_head_submit($post_ID) {
global $wpdb;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return $post_id;
}
// Check permissions
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
foreach((array)$_POST['post-head'] as $file) {
if ($file['bpm'] != "" && $file['duration'] != "") $t[] = $file;
$data[$post_ID] = $t;
}
update_option('post-head-files', serialize($data));
}
function post_head_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/post-header.js').'"></script>'."\n";
}
add_action('admin_head', 'post_head_admin_head');
add_action('admin_menu', 'post_head_menus');
add_action('save_post', 'post_head_submit');