Routing comments to a different database
-
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
$wpdbobject. 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 withwp_insert_comment()
Copy the read-only instance in the temp variable back to the global$wpdbThere’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.
Thanks for your suggestion. I will try this and let you know how this goes…
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 ?
The best approach is to create a plugin to contain your code. A basic plugin is simple to create, see 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.
$wpdbis a global instance of the 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() is in the link.
Other useful resources, is of course the Codex along with the developer reference linked previously. The Plugin Developer Handbook is also useful.
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.
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 🙂
The topic ‘Routing comments to a different database’ is closed to new replies.
(@vijiatcap1)
9 years, 11 months ago
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?