I’ve recently been asked for advice regarding developing a simple WordPress plugin that can perform explicit interactions with the database.
Having no prior experience of WordPress and very short experience of PHP (and this is dating 20 years back) I had no idea how one would tackle it. I was tempted to just say no, especially that PHP is one of the technologies I just can’t produce a spark of love towards. However, I said I will have a look, as curiosity got the best of me. I decided to time-box it to a day of research and prototyping. I got a working solution but I’m sure somebody knowing PHP and/or WordPress well, would point out a number of problems with it. Please do!!
I named the plugin “Pain Inducer” with apprehension of bad things that will happen if I start coding in PHP.
Complete code is available on GitHub.
If you have WordPress installed on your machine already you can skip to next section.
I was not keen to install the platform directly onto my machine so used Docker instead.
Note: This WordPress activity is probably just a one-off for me as I am happy with Jekyll. I don’t use PHP either so why cluttering my system with it? For experimentation like this Docker is brilliant, as I can easily discard the impact of this installation. I also get the environment setup scripted so it is repeatable. If you don’t have Docker but are tempted to try it, install it from here. For Docker-compose pop-in here. I’m on Linux machine where it runs natively, but if you are on Mac or Windows I believe you will need to use docker-machine, which (I think) ships with Docker.
Quick search revealed quickstart article on WordPress.
The platform consists of two services:
version: '3.3' services: db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql restart: always ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: somewordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on: - db image: wordpress:latest ports: - "8000:80" restart: always volumes: - ./my-plugins/pain_inducer:/var/www/html/wp-content/plugins/pain_inducer environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress volumes: db_data:
I’ve modified the quickstart
docker-compose.yml file as following:
To start them all, navigate to top directory (cloned from Git) and run:
~/src/wordpress-play$ docker-compose up
Docker has to download all images required to spin up the containers, so the first run will take some time. Subsequent invocations are instant. The command prompt will not appear again (foreground mode) so you have to look for a line similar to the following in your standard output:
wordpress_1 | Complete! WordPress has been successfully copied to /var/www/html
The above command will run in the foreground, which is easier to stop for novice Docker users (Ctrl-C once to
finish gracefully, again to kill it).
For background execution you can use
... up -d (to start) and
... down -v (to stop and cleanup).
There is more but it isn’t an article about Docker.
If you have WordPress installed and setup already you can skip to next section.
This step isn’t tying into the original advice request, but is included for completeness.
After you start WordPress for the first time you need to set it up. It applies to either option; direct local installation and containerised run.
Notice that the repo you’ve cloned from GitHub already contains the PHP files defining the plugin
It will be automatically found by WordPress because the
volumes field of
docker-compose.yml maps the host directory
/var/www/html/wp-content/plugins/pain_inducer in the container.
Note: if you don’t use Docker for this, copy the directory
pain-inducer and its contents immediately under
your WordPress’ plugins directory.
pain_inducer.php controls the logic of the plugin
dataset.php is providing means to interact with WordPress’ MySQL database.
Now you can just click on “Plugins” in the left nav you should see something like that:
You need to choose “Activate” under “Pain Inducer”, notice I’ve already done this here.
You are now ready to use the new shortcode!
Create new post and place the following inside it:
You have now made it possible to induce pain, well done!
When you save and preview you’ll get view similar to this:
Subsequent screens show adding two items:
After each submission the database is queried and brings back the newly added item.
The shortcode provides some customisation options (for demonstration).
You can provide a different prompt to the default one (as per the above print-screens):
[induce_pain]what kind of pain can I serve you today?[/induce_pain]
You can control the length of the listing fetched from the database:
And, finally, you can control the table to store the pain in:
This table (with applied prefix) will be created on plugin initialisation, if it does not exist.