Title: Routing comments to a different database
Last modified: September 1, 2016

---

# Routing comments to a different database

 *  [vijiatcap1](https://wordpress.org/support/users/vijiatcap1/)
 * (@vijiatcap1)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/routing-comments-to-a-different-database/)
 * Hi,
 * I have a secure site with admin behind a secure subnet. I am offloading media,
   assets to public S3 bucket using Offload S3 paid plugin. I have a read replica
   of the Admin DB that is connected to the customer facing WAF and EC2 servers.
 * I would like to enable commenting for this site. Now that the customer facing
   site only has a read only database associated with it, I would like to see what
   options I have. How about we expose an API that takes comments and posts to a
   service that can write to the Admin database?
 * Is there a better option?

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/routing-comments-to-a-different-database/#post-7462443)
 * You needn’t a formal API, you just need to alter which DB is written to as needed.
   This can be done by creating an instance of the wpdb class that connects to the
   admin DB. The read only DB is connected via the global `$wpdb` object. You can
   manage where the data goes by swapping wpdb DB connection instances in and out
   of that global.
 * The basic logic when a comment is submitted goes like this:
    Copy the global `
   $wpdb` (read-only DB connection) to a temp variable for safe keeping. Copy the
   admin DB instance to the global `$wpdb` Save the comment with `wp_insert_comment()`
   Copy the read-only instance in the temp variable back to the global `$wpdb`
 * There’s likely convenient hooks from where these swaps can occur. Even if not,
   the comment form could submit to a custom handler that manages the whole process
   instead of the default WP handler.
 * I’m assuming there is some mechanism for updating the read-only DB to synch with
   the admin DB, much like a caching system would do. Depending on how this is triggered,
   there may be a delay until the new comment shows up when the page is refreshed.
 *  Thread Starter [vijiatcap1](https://wordpress.org/support/users/vijiatcap1/)
 * (@vijiatcap1)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/routing-comments-to-a-different-database/#post-7462500)
 * Thanks for your suggestion. I will try this and let you know how this goes…
 *  Thread Starter [vijiatcap1](https://wordpress.org/support/users/vijiatcap1/)
 * (@vijiatcap1)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/routing-comments-to-a-different-database/#post-7462738)
 * Hello – We just started to work on your suggestion and would like to have some
   additional details on your suggestion…Which file do we modify to alter the wpdb
   variable? Can you point me to how/where this variable is set, so we can modify
   it exactly the same way? Where is the wp_insert_comment function ?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/routing-comments-to-a-different-database/#post-7462742)
 * The best approach is to create a plugin to contain your code. A basic plugin 
   is simple to create, see [Writing a Plugin](http://codex.wordpress.org/Writing_a_Plugin).
   It’s what goes into it (or not) that makes it simple, complex, clever, mundane,
   etc. All of your code would go in this plugin, there should be no need to alter
   any existing file. If you are altering someone else’s code, you are most likely
   doing it wrong. You may need to copy snippets and alter the copy, but never alter
   the original.
 * `$wpdb` is a global instance of the [wpdb](https://developer.wordpress.org/reference/classes/wpdb/)
   class, so declare the global, save it to a variable, then create a new instance
   that connects to the admin DB and assign it to the global.
 * All you need to know about [wp_insert_comment()](https://developer.wordpress.org/reference/functions/wp_insert_comment/)
   is in the link.
 * Other useful resources, is of course the [Codex](http://codex.wordpress.org/Main_Page)
   along with the developer reference linked previously. The [Plugin Developer Handbook](https://developer.wordpress.org/plugins/intro/)
   is also useful.
 *  Thread Starter [vijiatcap1](https://wordpress.org/support/users/vijiatcap1/)
 * (@vijiatcap1)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/routing-comments-to-a-different-database/#post-7462743)
 * thanks! Will try that. We will try this and circle back with you. We are new 
   to WordPress and it’s been quite a journey building a secure site for our business.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/routing-comments-to-a-different-database/#post-7462744)
 * Welcome to the WordPress community. It’s too bad your first WP hack is a rather
   unique situation where previous examples would be hard to find. OTOH, you will
   certainly pick up a lot of useful information along the way. Happy coding 🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Routing comments to a different database’ is closed to new replies.

## Tags

 * [Comments](https://wordpress.org/support/topic-tag/comments/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 6 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [9 years, 10 months ago](https://wordpress.org/support/topic/routing-comments-to-a-different-database/#post-7462744)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
