I’ve had multiple occasions where I just wanted to try something on a clean install in WordPress. Today we’re setting this up in Cloudways.
Don’t have a server running yet?
Click here to get started: https://www.cloudways.com/en/?id=713055
(yup, that’s an affiliate link)
My requirements
- An environment that resets itself every hour
- The database is reset, as well as everything in the wp-content folder
- Show a countdown timer in the top that shows how much time is left before it resets
Let’s get this ball rolling
We’ll start by creating a new application in Cloudways. I’ve called it wpsandbox and attached a domain, let’s call it wpsandbox.example.com. I’ve also added an SSL certificate.
Creating the reset script
Basically, we need to run two commands every 60 minutes, let’s start by adding a cronjob that runs a shell script. Login to your server via SSH and create a file called reset-sandbox.sh
in ~/scripts
.
mkdir ~/scripts
nano ~/scripts/reset-sandbox.sh
Inside of that file we’ll add some commands: (~/sites/wpsandbox is a symlink to ~/applications/APPLICATION_ID/public_html)
cd ~/sites/wpsandbox/wp db reset --yes
rm -rf wp-content/plugins/*
rm -rf wp-content/themes/*
rm -rf wp-content/uploads/*
wp core download --force
wp core install --url=https://wpsandbox.example.com --title="WP Sandbox" --admin_user=admin --admin_password=password [email protected]
Good, save and exit the editor with control-O and control-X.
Adding the script as a cronjob
Cloudways has a UI for adding cronjobs. Go to your application and click Cron Job Management in the sidebar. Open the Advanced tab and add the following line:0 * * * * /home/master/scripts/reset-sandbox.sh
Adding the countdown timer
Before getting into custom code, let’s see if there are any countdown timer plugins that work for us. There might be a plugin that we can re-configure automatically whenever the installation is reset.
After a bit of research, it doesn’t seem like there are plugins that add this code to the top, which is what I wanted. So I’ll write a quick must-use plugin that displays a shortcode which will be rendered by another plugin. I also need to make sure this plugin cannot be disabled and is re-installed on each new install.
I found a plugin called Event Countdown Timer here: https://wordpress.org/plugins/event-countdown-timer/ which suits our needs. We’ll use the following shortcode:[tme_countdown background_color="#fff" time_box_color="#fff" time_text_color="#000" time_title_color="#000" countdown_datetime="2021/05/08 17:04" countdown_timezone="UTC"]
The mu-plugin I came up with:
modify('+1 hour');
$date->setTime($date->format('H'), 0);
echo '