Created
December 6, 2022 19:35
-
-
Save oddjar/5c042279e65fb01a2b33532b3b90f2fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Delete all comments | |
Description: A custom plugin for deleting all comments from a WordPress site. | |
Version: 1.0 | |
Author: Your Name | |
*/ | |
/* | |
ChatGPT prompt: | |
Create scaffolding for a custom WordPress plugin called "Delete all comments", with the ability to delete all comments from the site. OOP with a construct method. Custom admin screen. Custom menu item. Include a button on the admin screen that deletes all comments from the site. | |
*/ | |
class Delete_All_Comments { | |
// Constructor | |
public function __construct() { | |
// Register a custom admin screen for the plugin | |
add_action( 'admin_menu', array( $this, 'admin_menu' ) ); | |
// Handle the "delete-all-comments" action | |
add_action( 'admin_init', array( $this, 'handle_actions' ) ); | |
} | |
// Register a custom admin screen for the plugin | |
public function admin_menu() { | |
add_menu_page( | |
'Delete all comments', // Page title | |
'Delete all comments', // Menu title | |
'manage_options', // Capability | |
'delete-all-comments', // Menu slug | |
array( $this, 'admin_screen' ), // Callback function | |
'dashicons-trash', // Icon | |
99 // Position | |
); | |
} | |
// Display the custom admin screen for the plugin | |
public function admin_screen() { | |
echo '<h1>Delete all comments</h1>'; | |
echo '<p>This plugin allows you to delete all comments from your WordPress site.</p>'; | |
echo '<p><a href="?page=delete-all-comments&action=delete-comments" class="button button-primary">Delete all comments</a></p>'; | |
} | |
// Handle the "delete-all-comments" action | |
public function handle_actions() { | |
if ( ! isset( $_GET['page'] ) || 'delete-all-comments' !== $_GET['page'] ) { | |
return; | |
} | |
if ( ! isset( $_GET['action'] ) || 'delete-comments' !== $_GET['action'] ) { | |
return; | |
} | |
// Delete all comments | |
$this->delete_comments(); | |
} | |
// Delete all comments from the site | |
public function delete_comments() { | |
// Get all comments | |
$comments = get_comments( array( | |
'number' => -1, | |
) ); | |
// Loop through all comments and delete them | |
foreach ( $comments as $comment ) { | |
wp_delete_comment( $comment->comment_ID, true ); | |
} | |
} | |
} | |
// Instantiate the plugin | |
new Delete_All_Comments(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment