Skip to content

Instantly share code, notes, and snippets.

@Qubadi
Last active September 5, 2024 12:12
Show Gist options
  • Save Qubadi/1d01184fc7c42355979f52e361ffa58e to your computer and use it in GitHub Desktop.
Save Qubadi/1d01184fc7c42355979f52e361ffa58e to your computer and use it in GitHub Desktop.
Sends an email notification if new updates are available for plugins and theme
This code defines a function send_update_notification in a WordPress environment.
It checks for updates to plugins and themes, and sends an email notification if new updates are available.
For plugins, it uses get_site_transient('update_plugins') to check for plugin updates, and for themes,
it uses get_site_transient('update_themes'). If updates are found and have not been previously notified
(based on options stored in the database), it sends an email to a specified address with details about
the available updates. The function is hooked to the admin_init action, so it runs during the initialization of
the WordPress admin panel.
function send_update_notification() {
$to = 'YOUR EMAIL';
$subject = 'New WordPress update available';
$headers = 'YOUR SITE NAME <YOUR EMAIL>' . "\r\n" . 'Reply-To: [email protected]' . "\r\n";
// Check for plugin updates
$plugin_updates = get_site_transient( 'update_plugins' );
if ( ! empty( $plugin_updates->response ) ) {
$notified_plugins = get_option( 'notified_plugin_updates', array() );
foreach ( $plugin_updates->response as $plugin ) {
if ( ! in_array( $plugin->slug, $notified_plugins ) ) {
$message = 'A new update is available for the ' . $plugin->slug . ' plugin. Please log in to your dashboard to update it.';
wp_mail( $to, $subject, $message, $headers );
$notified_plugins[] = $plugin->slug;
}
}
update_option( 'notified_plugin_updates', $notified_plugins );
}
// Check for theme updates
$theme_updates = get_site_transient( 'update_themes' );
if ( ! empty( $theme_updates->response ) ) {
$notified_themes = get_option( 'notified_theme_updates', array() );
foreach ( $theme_updates->response as $theme ) {
if ( ! in_array( $theme->slug, $notified_themes ) ) {
$message = 'A new update is available for the ' . $theme->slug . ' theme. Please log in to your dashboard to update it.';
wp_mail( $to, $subject, $message, $headers );
$notified_themes[] = $theme->slug;
}
}
update_option( 'notified_theme_updates', $notified_themes );
}
}
add_action( 'admin_init', 'send_update_notification' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment