Marinus Klasen

Marinus Klasen

  • Development
  • Consultancy
  • Developer Resources
  • Blog
  • Contact

May 8, 2021

Creating a WordPress Sandbox environment that resets hourly with Cloudways

Marinus Klasen

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:

<?php
add_action('wp_body_open', function() {
        $date = new Datetime();
        $date->modify('+1 hour');

        $date->setTime($date->format('H'), 0);

        echo '<style>.tme-countdown {max-width: 100%; background: #fff; display: flex; align-items: center; justify-content: center; font-size: 12px; } .tme-countdown-container { width: auto !important;} .tme-countdown .widget-title { font-size: 15px; }$
        echo do_shortcode('[tme_countdown title="This sandbox will reset in:" img_link="" background_color="#fff" time_box_color="#fff" time_text_color="#000" time_title_color="#000" countdown_datetime="'.$date->format('Y/m/d H:i').'" countdown_timezone$
});

add_action('admin_init', function() {
        if ( !is_plugin_active('event-countdown-timer/tme-countdown.php')) {
                activate_plugins('event-countdown-timer/tme-countdown.php');
        }
});

The last thing we need to do is install this plugin on every wipe. Let’s add the following line to our bash script:
wp plugin install event-countdown-timer --activate

That’s it! Since our cronjob is set to run every hour, and the countdown timer is set to countdown to the next hour, they’ll work together flawlessly.

About Marinus Klasen

Marinus has been working in software/web development for more than a decade. Since 2020 his attention shifted on sharing knowledge and developing products and tools for sharing knowledge.

Marinus Klasen on Twitter

This site runs on Cloudways.
It's fast isn't it?
Cloudways offers high-quality, fast and affordable hosting.

Learn more

Need a hand? Post your project and hire me and other top notch developers on Codeable.

Hire me on Codeable

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Ready to take action?

I'm looking forward to discuss your projects and goals!
Feel free to reach out using the contact details below.

Marinus Klasen

[email protected]
twitter.com/marinusklasen
linkedin.com/in/marinusklasen

  • GitHub
  • LinkedIn
  • Twitter

Have you read?

  • Storing private data with SSH on WPEngine sitesDecember 15, 2022
  • Get the HTML content of a block edit page or post in WordPressNovember 30, 2022
  • Rename Coupon code text to Discount code in WoocommerceNovember 3, 2022
  • Background-size cover in mPDFOctober 22, 2022
  • WordPress.com SSH & duplicating a WordPress.com website locallyOctober 11, 2022

Copyright © 2023 · Marinus Klasen | Webdesign by Team Rood