here is a quick plugin i cooked for you 🙂
<?php
/*
Plugin Name: Post Auto Removal
Plugin URI: http://en.bainternet.info
Description: Post Auto Removal lets you schedule when the post / page / custom type will be deleted automatically.
Version: 0.1
Author: bainternet
Author URI: http://en.bainternet.info
*/
/* hook meta box */
add_action("admin_init", "admin_init");
/* hook meta box function */
function admin_init(){
add_meta_box("Post Auto Removal", "Post Auto Removal", "Post_Auto_Removal_options", "post", "normal", "high");
add_meta_box("Post Auto Removal", "Post Auto Removal", "Post_Auto_Removal_options", "page", "normal", "high");
}
/* display meta box */
function Post_Auto_Removal_options() {
global $post;
$custom['active_removal'] = get_post_meta($post->ID,'active_removal',true);
$custom['Remove_after'] = get_post_meta($post->ID,'Remove_after',true);
echo '<input type="hidden" name="wp_meta_box_nonce" value="'. wp_create_nonce('Auto_Removal'). '" />';
?>
<table border=0>
<tr>
<th style="width:20%"><label for="active_removal">Activate auto removal:</label></th>
<td><input type="checkbox" name="active_removal" id="active_removal" value="yes" <?php echo $custom['active_removal'] ? 'checked' : ''; ?>/><br/>
when check post will be deleted in the given time.
</td>
</tr>
<tr>
<th style="width:20%"><label for="Remove_after">Delete post after:</label></th>
<td><input type="text" name="Remove_after" id="Remove_after" value="<?php echo $custom['Remove_after'] ? $custom['Remove_after'] : ''; ?>"/><br/>
Enter time in Seconds Ex: 1 Hour = 3600 Seconds , 1 Day = 86400 Seconds.
</td>
</tr>
<?php
$next = wp_get_schedule( 'Clean_my_posts', array($post_id));
print_r($next);
if ($next){ ?>
<tr>
<th>next schedule is set to : <?php echo $next; ?></th>
</tr>
<?php } ?>
</table>
<?php }
/* save meta box hook*/
add_action('save_post', 'save_Post_Auto_Removal_options');
/* save meta box function*/
function save_Post_Auto_Removal_options($post_id) {
if (!wp_verify_nonce($_POST['wp_meta_box_nonce'], "Auto_Removal")) {
return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
If (isset($_POST['Remove_after']) && isset($_POST['active_removal'])){
//cerate scheduled event
$time = time() + $_POST['Remove_after'];
wp_schedule_single_event($time, 'Clean_my_posts',array($post_id));
//save meta data
update_post_meta($post_id, 'Remove_after', $_POST['Remove_after']);
update_post_meta($post_id, 'active_removal', $_POST['active_removal']);
}
}
/* hook removal event function */
add_action('Clean_my_posts','auto_remove_post',1,1);
// the function that deletes a post everything that is tied to it, This includes comments, post meta fields, and terms associated with the post.
function auto_remove_post($post_id) {
$delete = get_post_meta($post_id, 'active_removal', true);
if ($delete){
wp_delete_post( $post_id, true );
}
}