Intro
While laziness may seem like a hindrance, lazy developers actually bring immense value to any project. They are always looking for ways to avoid doing repetitive, menial tasks which lead to creative automated solutions, which then improve team efficiency and save time in the long run.
Today, I'll show you how laziness can boost your productivity and allow you to focus on things that matter while building WordPress solutions. You'll learn:
- How to prepare to task automation in Wordpress?
- How to automate WordPress installation?
- How to automate database management?
Tools
There are many possibilities to automate tasks in WordPress depending on yor personal workflow. I'll show you two tools without which I cannot imagine this type of work.
Bash
The easiest way for me is creating simple scripts using Bash. It is a command-line shell and scripting language for Unix systems, used to interact with the them by entering commands and executing scripts.
So I create them using simple and easy-to-read pattern that allows me to fire different tasks just by passing their names as parameters. It reads .env
file so all project variables are also avaliable there. Maybe that’s nothing fancy but it works nice.
#!/bin/bash
# Read and inject variables from .env file
if [[ ! -f "./.env" ]]; then
echo "Missing .env file."
else
export $(grep -v '^#' .env | xargs)
fi
# Define specic tasks
function task() {
echo "Performing task #1..."
}
# Switch statement that fires specific function from parameter
case $1 in
"task")
task
;;
esac
To perform the operations in the task
function I just need to use wp.sh
script and pass task name as parameter: ./wp.sh task
.
WP CLI
WP-CLI is a must-have for automating things in WordPress. It's a command-line tool that allows to manage WordPress using a terminal. It can perform many types of tasks, such as updating plugins and themes, creating new posts and pages, managing users and many many more.
Use Cases
What are typical tasks in my WordPress workflow that are boring and really repetitive for me which I try to automate? Be aware that those scripts are created based on my workflow, so check them carefuly and adjust to your needs if you want to use them.
Summary
Using the power of laziness to automate tasks in WordPress with Bash
and WP CLI
can really improve development efficiency and team productivity. By reducing an impact of repetitive and time-consuming tasks, we can save time and focus on more important aspects of the project.
I understand that there could be much more thing to automate in WordPress, but I just want to show that if you do the same processes often, probably there's way to automate them and gain more time for things that interest you more!
Notes
If you want to use full version of the script analyzed in this example, feel free to check out GitHub or just use the command below to download it and make executable. Enjoy!
curl https://raw.githubusercontent.com/przemekhernik/templates/main/bash/wp.sh -o wp.sh && chmod 744 wp.sh