Last active
December 12, 2024 15:14
-
-
Save petertwise/795b67ee31cf99a1da0eb5980090c1ec to your computer and use it in GitHub Desktop.
WP Total Disk Size Alert
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: WP Total Disk Size Alert | |
Description: Alerts when the total size of the WordPress installation exceeds a given size. | |
Version: 1.0.0 | |
Author: Square Candy | |
* Defaults to a 10GB limit | |
* Defaults to sending the alert to the site admin email | |
* Sends the alert when the total size is within 1GB of the limit | |
* Runs daily | |
To adjust the limit or email address, define the following constants in wp-config.php: | |
define( 'WP_SIZE_ALERT_LIMIT', 20 ); // set the limit to 20GB | |
define( 'WP_SIZE_ALERT_EMAIL', '[email protected]' ); // send the alert to the client's email | |
define( 'WP_SIZE_SUPPORT_EMAIL', '[email protected]' ); // set a support email address | |
*/ | |
function squarecandy_check_wp_size() { | |
if ( ! class_exists( 'WP_Debug_Data' ) ) { | |
require_once ABSPATH . 'wp-admin/includes/class-wp-debug-data.php'; | |
} | |
$sizes_data = WP_Debug_Data::get_sizes(); | |
$total_size = $sizes_data['total_size']['raw'] ?? false; | |
if ( ! $total_size ) { | |
return; | |
} | |
// Set the alert theshold - fallback to 10GB if not set | |
if ( defined( 'WP_SIZE_ALERT_LIMIT' ) ) { | |
$size_limit = WP_SIZE_ALERT_LIMIT; | |
} else { | |
$size_limit = 10; | |
} | |
// send the alert when the size is approaching the limit (1GB under) | |
$threshold = $size_limit - 1; | |
// threshold GB to bytes | |
$threshold = $threshold * 1024 * 1024 * 1024; | |
if ( $total_size > $threshold ) { | |
if ( defined( 'WP_SIZE_ALERT_EMAIL' ) ) { | |
$alert_email = WP_SIZE_ALERT_EMAIL; | |
} else { | |
$alert_email = get_option( 'admin_email' ); | |
} | |
// Send an alert email | |
$subject = 'Website Storage Size Alert'; | |
$message = 'The total storage size of your website is approaching the limit of ' . $size_limit . "GB.\r\n\r\n"; | |
$message .= 'Current size: ' . size_format( $total_size, 2 ) . "\r\n\r\n"; | |
$message .= 'Please be aware that you may be moved to a higher storage pricing tier once your site exceeds the limit.' . "\r\n\r\n"; | |
$message .= defined( 'WP_SIZE_SUPPORT_EMAIL' ) ? 'If you have any questions or concerns, please contact ' . WP_SIZE_SUPPORT_EMAIL . '.' : ''; | |
wp_mail( $alert_email, $subject, $message ); | |
} | |
} | |
// Schedule the size check to run daily | |
if ( ! wp_next_scheduled( 'squarecandy_check_wp_size_daily' ) ) { | |
wp_schedule_event( time(), 'hourly', 'squarecandy_check_wp_size_daily' ); | |
} | |
add_action( 'squarecandy_check_wp_size_daily', 'squarecandy_check_wp_size' ); | |
// Clear the scheduled event upon plugin deactivation | |
function squarecandy_wp_size_alert_deactivate() { | |
wp_clear_scheduled_hook( 'squarecandy_check_wp_size_daily' ); | |
} | |
register_deactivation_hook( __FILE__, 'squarecandy_wp_size_alert_deactivate' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment