06 Mar

Fruitful Code Theme Warnings

I use the fruitful theme by fruitfulcode on my WordPress blog. After a recent change of environment, which meant that non-fatal PHP warnings would be displayed, I noticed warnings being displayed at the top of the blog

I navigated to the code highlighted in the warning (line 756 of inc/metaboxes/init.php) and found this

elseif ( is_array( $meta_box['pages'] ) && count( $meta_box['pages'] === 1 ) )

It is the second condition in this if statement that causes a problem. As we are invoking count on a boolean (the results of $meta_box['pages'] === 1) rather than on an array or countable object PHP will emit this warning. I assume that the developer actually wanted to compare the number of pages to 1 and so I moved the close parenthesis so that we are invoking count on $meta_box['pages'], which we know is an array from the first condition in the if statement.

elseif ( is_array( $meta_box['pages'] ) && count( $meta_box['pages'] ) === 1 )

After this code update the warnings disappeared. As I do not find it best practice to modify third party code I reverted my change and instead submitted it as a pull request to the fruitful theme developers.

06 Mar

WordPress Docker containers and display of PHP warnings

I use a Docker container to host a WordPress blog and I recently upgraded the Docker image to one based on an image which used PHP 7.2. I had previously been using one based on Apache with PHP 5.6.

I immediately noticed warnings being displayed at the top of the blog

The warnings where from PHP letting a developer know that their code had a non fatal error. The cause of the error is a subject for another blog post. I just wanted to turn off the display of these warning messages.

To do this you can set the display_errors configuration variable to Off in php.ini

Create a file called hide_errors.ini with the following content

display_errors = Off

I then proceeded to recreate my WordPress docker container with the same volume as the current container used, as well as mounting hide_errors.ini to /usr/local/etc/php/conf.d/hide_errors.ini inside the container.

After this the warnings were no longer displayed.

29 Jul

Containerising your Word Press blog with Docker

In this post, I will talk you through how you can create a copy of your live Word Press blog as a Docker container. The reason I wanted to do this, is so that I can test actual updates and themes to my live site before making them publicly available and seeing how any tested plug-ins interact with actual live content.

The first thing you need to do is make sure you have Docker installed or have access to a Docker host.

Pull down the Word Press, MySQL and PhpMyAdmin prebuilt Docker images from the Docker hub

docker pull wordpress
docker pull mysql
docker pull phpmyadmin/phpmyadmin

Just to note at the time of writing this post the tags for each where:

Image Tag Dockerfile
wordpress 4.5 https://github.com/docker-library/wordpress/blob/6afa0720da89f31d6c61fd38bb0d6de6e9a14a49/apache/Dockerfile
mysql 5.7.13 https://github.com/docker-library/mysql/blob/f7a67d7634a68d319988ad6f99729bfeaa84ceb2/5.7/Dockerfile
phpmyadmin/phpmyadmin 4.6.3-1 (from GitHub tag) https://github.com/phpmyadmin/docker/blob/4.6.3-1/Dockerfile

We now need to download a snapshot of your current live word press blog. This is very hosting company specific but in the lowest generic terms possible you need to download all files that come under the www root directory on your hosting companies servers and to export a copy of your Word Press MySQL database. In the rest of this post I will refer to the copy of the www root directory as the Word Press files and I will refer to the export of the MySQL database as the database.

At this point we need to run up a MySQL and PhpMyAdmin container. Execute the following on the Docker host.

docker run --name wp-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pwd -d mysql
docker run --name phpmyadmin -d --link wp-mysql:db -p 8081:80 phpmyadmin/phpmyadmin

A MySQL server instance will now be running on the Docker host and it can be administered from the PhpMyAdmin instance running on port 8081 of the Docker host.

Use the PhpMyAdmin instance with the MySQL root password set above to create a new user and make sure you select to create a database with the same name. After this, import your database files using the import option of PhpMyAdmin.

Once you have imported the database modify the kvht_options table so that the records with the following name are updated accordingly:

name value
siteurl <docker-host-hostname-or-IP-address>:8080
home <docker-host-hostname-or-IP-address>:8080
upload_path /var/www/html/wp-content/uploads

Note: The table name may have a different prefix to mine or no prefix at all.

At this point if you have any media which is embedded using an absolute URL to your actual live Word Press site then you need to update individual posts to make the URL’s server relative. This is not taken care of in this post as I always use server root relative paths for this type of media. A quick trick would be to do an appropriate regex find and replace in the exported SQL file before the import.

Now we need to run an instance of the Word Press image but using our Word Press files.

docker run --name wp-blog --link wp-mysql:mysql -p 8080:80 -e MYSQL_ENV_MYSQL_USER=database_user -e MYSQL_ENV_MYSQL_PASSWORD=database_user_password -e MYSQL_ENV_MYSQL_DATABASE=database_name -v /path/to/wordpress/files:/var/www/html -d wordpress

At this point you should be able to access your Word Press site on port 8080 of the Docker host.

For those that use boot2docker (even through docker-machine) your Word Press file directory may not be writeable by the Word Press docker container. To fix this I built the Word Press image directly from the associated Dockerfile with an extra command to modify the www-data user inside the image so that it’s UID matched a user’s UID on the source file systems directory that had write access to the Word Press files.