Configure WordPress to read from database only, never write

WordPress doesn’t work well in read-only mode. It depends on the database for cache and other tasks.

However, there are a couple of options if you’d still like to proceed.

  1. Set the database user permissions to ‘READ ONLY’
  2. Use a function to intercept all db queries. We used this on our theme demo site to prevent db writes.

Function to use:

/**
 * Whitelist "SELECT" and "SHOW FULL COLUMNS" queries.
 */
function my_readonly_filter( $query ) {
  global $wpdb;

  if ( preg_match( '/^\s*select|show full columns\s+/i', $query ) ) {
    return $query;
  }

  // Return arbitrary query for everything else otherwise you get 'empty query' db errors.
  return "SELECT ID from $wpdb->posts LIMIT 1;";
}
add_filter( 'query', 'my_readonly_filter' );

Leave a Comment