Skip to content

Instantly share code, notes, and snippets.

@solepixel
Last active December 17, 2015 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solepixel/5688252 to your computer and use it in GitHub Desktop.
Save solepixel/5688252 to your computer and use it in GitHub Desktop.
Delete Them All Plugin for WordPress
<?php
/**
* Plugin Name: Delete Them All!
* Plugin URI: http://infomedia.com
* Description: Delete ALL posts and associated data, leaving your database nice and clean!
* Version: 1.0
* Author: Brian DiChiara
* Author URI: http://www.briandichiara.com
*/
define('DTA_VERSION', '1.0');
define('DTA_PI_NAME', 'Delete Them All!');
define('DTA_PI_DESCRIPTION', 'Delete ALL posts and associated data, leaving your database nice and clean!');
define('DTA_OPT_PREFIX', 'dta_');
define('DTA_PATH', plugin_dir_path( __FILE__ ));
define('DTA_DIR', plugin_dir_url( __FILE__ ));
add_action('admin_init', 'dta_admin_init');
/**
* dta_admin_init()
*
* @return void
*/
function dta_admin_init(){
dta_delete_them_all();
add_action('restrict_manage_posts', 'dta_link');
}
/**
* dta_link()
*
* @return void
*/
function dta_link(){
global $typenow, $current_user;
get_currentuserinfo();
if($current_user->allcaps['delete_posts'] === true){
$post_type = isset($_REQUEST['post_type']) ? $_REQUEST['post_type'] : $typenow;
if($post_type && have_posts()){
echo '<style type="text/css">.button.delete-them-all { margin-right:5px; }</style>';
echo '<script type="text/javascript">jQuery(function($){$(\'.delete-them-all\').click(function(){ var count = $(\'.displaying-num:first\').text(); var conf = confirm(\'Are you sure you want to delete ALL \'+count+\'? There is no undo.\'); return conf;}); });</script>';
echo '<a href="'.add_query_arg(array('delete-them-all' => $post_type), admin_url('edit.php')).'" class="button delete-them-all">'.__('Delete Them All!', 'delete-them-all').'</a>';
}
}
}
/**
* dta_delete_them_all()
*
* @return void
*/
function dta_delete_them_all(){
if(isset($_REQUEST['delete-them-all']) && $_REQUEST['delete-them-all']){
global $wpdb, $current_user;
get_currentuserinfo();
if($current_user->allcaps['delete_posts'] === true){
set_time_limit(120);
$post_type = $_REQUEST['delete-them-all'];
if($post_type){
$post_type_object = get_post_type_object( $post_type );
$deleted = 0;
$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT `ID` FROM $wpdb->posts WHERE post_type=%s", $post_type ) );
foreach($post_ids as $post_id){
if ( !current_user_can($post_type_object->cap->delete_post, $post_id) )
wp_die( __('You are not allowed to delete this item.') );
if ( !wp_delete_post($post_id) )
wp_die( __('Error in deleting...') );
$deleted++;
}
}
}
$args = array('post_type' => $post_type);
if($deleted){
$args['deleted'] = $deleted;
}
$redirect = add_query_arg($args, admin_url('edit.php'));
wp_redirect($redirect);
exit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment