#!/bin/bash
# Clean up the database
sudo -u www-data wp db optimize
# Update WordPress core, themes, and plugins
sudo -u www-data wp core update
sudo -u www-data wp plugin update --all
sudo -u www-data wp theme update --all
Replace www-data with the username of the sudo user you want to use. Save the script as a file (e.g., update-wordpress.sh), and make it executable by running the following command:
chmod +x update-wordpress.sh
Then, you can execute the script by running:
sudo -u your_sudo_user ./clean-wordpress.sh
Note that you’ll need to have WP-CLI installed and configured on your system for these commands to work, and the specified sudo user must have permission to execute the wp command.
This Sample PHP script update WordPress and all its plugins automatically and send an email notification if the update is successful, or if there are any errors during the update process.
<?php
// Set the email address to receive notifications
$to = "your_email@example.com";
// Set the WordPress directory path
$wp_dir = "/path/to/wordpress/";
// Get the latest version of WordPress
$wp_latest_version = file_get_contents("https://api.wordpress.org/core/version-check/1.7/");
// Check if there are any updates available
$update = false;
if (version_compare($wp_latest_version['offers'][0]['version'], $wp_version, '>')) {
$update = true;
}
// Update WordPress and plugins if an update is available
if ($update) {
// Require WordPress core update files
require_once($wp_dir . "wp-load.php");
require_once($wp_dir . "wp-admin/includes/admin.php");
require_once($wp_dir . "wp-admin/includes/class-wp-upgrader.php");
require_once($wp_dir . "wp-includes/update.php");
// Create a new WordPress Upgrader object
$upgrader = new WP_Upgrader();
// Update WordPress
$wp_update_result = $upgrader->run(array(
'package' => $wp_latest_version['packages']['full'],
'destination' => ABSPATH,
'clear_destination' => false,
'clear_working' => true,
'is_multi' => false,
'hook_extra' => array()
));
// Update plugins
$plugins_update_result = $upgrader->bulk_upgrade();
// Send email notification with update result
if ($wp_update_result === true && !in_array(false, $plugins_update_result)) {
$subject = "WordPress and plugins have been updated successfully";
$message = "WordPress and all plugins have been updated successfully.";
} else {
$subject = "WordPress and plugins update has failed";
$message = "WordPress and/or some plugins failed to update. Please check the error messages below:\n\n";
if ($wp_update_result !== true) {
$message .= "WordPress update error message: " . $wp_update_result->get_error_message() . "\n";
}
foreach ($plugins_update_result as $plugin_name => $update_result) {
if ($update_result === false) {
$message .= "Plugin " . $plugin_name . " update error message: " . $upgrader->skin->result['errors'][$plugin_name]->get_error_message() . "\n";
}
}
}
mail($to, $subject, $message);
}
?>
Note that you’ll need to modify the script to include your own email address and the correct WordPress directory path. Also, make sure that the script is run periodically, either through a cron job or a scheduled task, to ensure that WordPress and its plugins are always up to date.