0
X
Add Snippet To Project
New Project
Add To Existing Project
/**
* Plugin Name: WPTurbo Remove Default Content
* Plugin URI: https://www.example.com/
* Description: Removes all default content from a fresh WordPress installation.
* Version: 1.0
* Author: WPTurbo AI
* Author URI: https://www.example.com/
* Text Domain: wpturbo
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Define plugin activation hook
register_activation_hook( __FILE__, 'wpturbo_remove_default_content_activation' );
// Define plugin deactivation hook
register_deactivation_hook( __FILE__, 'wpturbo_remove_default_content_deactivation' );
/**
* Runs on plugin activation to remove all default content from a fresh WordPress installation.
*/
function wpturbo_remove_default_content_activation() {
// Check if plugin has already been activated
if ( get_option( 'wpturbo_remove_default_content_activated' ) ) {
return;
}
// Delete all posts
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'any',
);
$posts = get_posts( $args );
foreach( $posts as $post ) {
wp_delete_post( $post->ID, true );
}
// Delete all pages
$args = array(
'posts_per_page' => -1,
'post_type' => 'page',
'post_status' => 'any',
);
$pages = get_posts( $args );
foreach( $pages as $page ) {
wp_delete_post( $page->ID, true );
}
// Delete all comments
$args = array(
'number' => -1,
'status' => 'approve',
);
$comments = get_comments( $args );
foreach( $comments as $comment ) {
wp_delete_comment( $comment->comment_ID, true );
}
// Set option to indicate plugin has been activated
update_option( 'wpturbo_remove_default_content_activated', true );
// Display notice inviting users to disable the plugin after use
add_action( 'admin_notices', 'wpturbo_remove_default_content_notice' );
}
/**
* Runs on plugin deactivation to delete the option that indicates the plugin has been activated.
*/
function wpturbo_remove_default_content_deactivation() {
delete_option( 'wpturbo_remove_default_content_activated' );
}
/**
* Displays a notice inviting users to disable the plugin after use.
*/
function wpturbo_remove_default_content_notice() {
$message = __( 'The WPTurbo Remove Default Content plugin has completed its task. For security reasons, it is recommended that you disable or delete this plugin now.', 'wpturbo' );
printf( '%1$s', esc_html( $message ) );
}