Skip to content

Instantly share code, notes, and snippets.

@dingo-d
Last active February 7, 2019 14:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dingo-d/b59e018f8631824dc2b4 to your computer and use it in GitHub Desktop.
Save dingo-d/b59e018f8631824dc2b4 to your computer and use it in GitHub Desktop.
<?php
/**
* !!WARNING!!
* This script will delete everything you have in wordpress.
* I use it only when developing something that requires me to delete everything, e.g. importer.
* You can comment some parts out if you need to quickly delete just posts, or pages or so, but still be very careful!!!
*
* @author Denis Žoljom (https://github.com/dingo-d)
* @license Free to use at your own risk
* @version 1.0.0
*
*/
//Fire the delete all function after all the plugins and theme are fully loaded to catch all the custom post types and terms too.
add_action('wp_loaded', 'delete_all');
if ( !function_exists( 'delete_all' ) ) {
function delete_all() {
global $wpdb;
//Get all the post types, terms, menus etc.
$menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
$all_post_types = get_post_types();
$all_taxonomies = get_taxonomies();
$all_options = wp_load_alloptions();
//Delete all options
foreach ( $all_options as $option ) {
delete_option( $option );
}
//Delete all menus
foreach ( $menus as $menu => $menu_obj ) {
wp_delete_nav_menu( $menu_obj->term_id );
}
//Delete all post types, that includes custom post types as well
foreach ( $all_post_types as $post_type => $post_type_value ) {
$temp_posts = get_posts( array( 'post_type' => $post_type, 'posts_per_page' => -1, 'post_status' => 'any' ) );
foreach ( $temp_posts as $temp_post => $temp_post_value ) {
wp_delete_post( $temp_post_value->ID, true );
}
}
//Delete any customizer modifications, if you use other theme option frameworks, you'll need to add the code to remove them manually
remove_theme_mods();
//Delete all terms from custom taxonomy. Taken from here: http://wpsmith.net/2014/plugin-uninstall-delete-terms-taxonomies-wordpress-database/
foreach ( $all_taxonomies as $taxonomy ) {
// Prepare & excecute SQL - also deletes Uncategorized from posts category!
$terms = $wpdb->get_results( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ( '%s' ) ORDER BY t.name ASC", $taxonomy ) );
// Delete Terms
if ( $terms ) {
foreach ( $terms as $term ) {
$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $term->term_taxonomy_id ) );
$wpdb->delete( $wpdb->term_relationships, array( 'term_taxonomy_id' => $term->term_taxonomy_id ) );
$wpdb->delete( $wpdb->terms, array( 'term_id' => $term->term_id ) );
delete_option( 'prefix_' . $taxonomy->slug . '_option_name' );
}
}
// Delete Taxonomy
$wpdb->delete( $wpdb->term_taxonomy, array( 'taxonomy' => $taxonomy ), array( '%s' ) );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment