To delete duplicate cron events with the same name in WordPress, you can use a custom function to check for and remove the duplicates. Here’s a step-by-step guide:
Step 1:
Access Your WordPress Theme’s Functions.php File In your WordPress dashboard, navigate to “Appearance” > “Theme Editor.” On the right-hand side, find and click on “Theme Functions (functions.php)”.
Step 2:
Create a Custom Function Add the following custom PHP function to your theme’s functions.php file:
function remove_duplicate_cron_events( $schedules ) {
global $wpdb;
$cron_events = $wpdb->get_results( "SELECT * FROM $wpdb->options WHERE option_name LIKE 'cron%'" );
$cron_event_counts = array();
$duplicate_cron_events = array();
foreach ( $cron_events as $event ) {
$event_name = str_replace( 'cron_', '', $event->option_name );
$event_name_parts = explode( '_', $event_name );
$event_count = count( $event_name_parts );
if ( isset( $cron_event_counts[ $event_name ] ) ) {
// Duplicate cron event found
$duplicate_cron_events[] = $event->option_name;
} else {
$cron_event_counts[ $event_name ] = $event_count;
}
}
foreach ( $duplicate_cron_events as $event_name ) {
// Delete the duplicate cron event
wp_clear_scheduled_hook( str_replace( 'cron_', '', $event_name ) );
}
}
add_action( 'init', 'remove_duplicate_cron_events' );
Step 3:
Save and Update the Functions.php File Save the changes to your theme’s functions.php file by clicking the “Update File” button.
Step 4:
Check for Deleted Duplicate Cron Events Once you have saved the functions.php file, the custom function will run on the “init” action and remove any duplicate cron events with the same name.
Important Note: Always create a backup of your theme’s functions.php file before making any changes. If you are not comfortable editing the functions.php file directly, consider using a child theme to avoid any accidental errors during updates.
This custom function will help you delete duplicate cron events with the same name, ensuring a more efficient and organized WordPress cron job system.