Nk_wp_cron_disable
Description
This is a bash script function called nk_wp_cron_disable()
that expects to be passed a domain name as an argument. The function disables the default WordPress cron
and sets up a system cron job to execute the WordPress cron every 12 hours. The function first defines some variables such as the docroot, user, php_version, wp_config_file, wp_cron_file, system_cron_file, and cron_cmd.
The function then calls two helper functions: update_config_file() and update_system_cron(). The update_config_file() function checks if the “DISABLE_WP_CRON” constant is already present in the site’s wp-config.php
file. If it is not present, the function adds the constant to the file. The update_system_cron()
function checks if there is already a cron job for the site’s wp-cron.php file in the user’s crontab. If there is, the function updates the existing cron job with the new values. If there is no existing cron job, the function adds a new one to the crontab.
The function prints out various messages to indicate the progress and status of the process. If the function is called without a domain name argument, it prints an error message and returns.
Examples
[root@cloudvpsserver ~]# nk_wp_cron_disable nkern.net DISABLE_WP_CRON already present at /home/nkern/public_html/wp-config.php Cron tab updated for: nkern.net at /var/spool/cron/nkern 0 0,12 * * * /usr/local/bin/ea-php81 /home/nkern/public_html/wp-cron.php >/dev/null
Code
nk_wp_cron_disable() { # nk_wp_cron_disable expects to be passed a domain. # print mesage and exit if domain not provide. if [ "$1" = "" ]; then echo "You must provide a domain name." return 0 fi # First we need to define some variables we'll be using. # domain : this is provided by the nk_user as an arguement. # docroot : this is the docroto associated with the domain found by running nk_docroot on the domain. # user : this is the user associated with the domain, found using nk_user. # php_version : this is the php version associated with the site, found with nk_php_version. # wp_config_file : this is the wp-config.php of the site. Made by appending wp-config.php to the end of docroot. # wp_cron_file : this is the wp-cron.php file. Made by appending wp-cron.php to the docroot. # system_cron_file : The name of the user's crontab, it's /var/spool/$user # cron_cmd : The cron command that we want to append. domain="$1" docroot="$(nk_docroot "$domain")" user="$(nk_user "$domain")" php_version="$(nk_php_version "$domain")" wp_config_file="$docroot/wp-config.php" wp_cron_file="$docroot/wp-cron.php" system_cron_file="/var/spool/cron/$user" cron_cmd="0 0,12 * * * /usr/local/bin/$php_version $wp_cron_file >/dev/null" update_config_file() { # Check if DISABLE_WP_CRON is in the site's wp-config.php. if [ "$(grep -ic "DISABLE_WP_CRON" "$wp_config_file")" != "0" ]; then # If it's there, print message and don't modify anything. and exit. echo "DISABLE_WP_CRON already present at $wp_config_file" return 0 fi # If it isn't there. Add it to the file, and print a message. echo "define('DISABLE_WP_CRON', true);" >> "$wp_config_file" echo "DISABLE_WP_CRON added to $wp_config_file" } update_system_cron() { # Check if there's a cron for the site path already in the crontab. if [ "$(grep -ic "$wp_cron_file" "$system_cron_file")" != "0" ]; then # If it does exist, update it to new values. # Use sed to remove the line where the existing cron entry is. sed -i "\|$docroot|d" "$system_cron_file" # Append a new crontab to the crontab file. echo "$cron_cmd" >> "$system_cron_file" # Print message stating crontab has been edited. echo "Cron tab updated for: $domain at $system_cron_file" # Print out the line that has been added. tail -1 "$system_cron_file" # Then exit. return 0 fi # If there's no crontab present whatsoever for the install, then append to crontab. echo "$cron_cmd" >> "$system_cron_file" # Print message stating the crontab has been edited. echo "Cron tab added for: $domain at $system_cron_file" # Print out the line that's been added. tail -1 "$system_cron_file" } update_config_file update_system_cron }