Friday, May 29, 2015

Generate static html file from WordPress page

OK, Is your WordPress page rarely get updated, let's say once a day or a week. If so, you can use the following technique to reduce the load (queries) to the database especially in multisite deployment:

Write a script to generate static content with updates from the database only once a day or a week. Then your users will access this static page instead of the heavy loading WordPress page with hundreds of database queries.


1. Create a wordpress template file wp_template.php (place it in your template root, e.g. /var/www/wordpress/wp-content/themes/mythemes/) which will get data from the database. For example:



2. Create a crontab to run this command every day at 1AM to generate the static html file in the web root:

/usr/local/bin/blog_static_gen.sh:

#! /bin/bash
$ cd /var/www/wordpress/wp-content/themes/mythemes
$ php wp_template.php > /var/www/wordpress/wp_template.html

crontab:

0 1 * * * /usr/local/bin/blog_static_gen.sh

3. Then you can access the static content at this URL:

http://mydomain.com/wp_template.html