I'm curious as to whether the following is possible as there are concerns that the WP site in question may be hacked via the front end & through a plugin..

  1. We want to have the front-end connect via a Read-Only DB user
  2. We want the admin area to have Read/Write access but have this URL protected by the server firewall rule that only allows internal traffic

I'm open to other suggestions

  • so basically if the url contains /wp-admin/ then it should connect to different DB ( for admin purpose ) and else it should connect to other DB ( for read purpose ) . If this is the case, then yes it is possible
    – Aftab
    Commented Apr 26, 2017 at 11:08
  • Yes, do you have a tutorial a plugin or reference as to how to implement this?
    – Daniel
    Commented Apr 26, 2017 at 11:14
  • I had setup a same website . Please check the answer section.
    – Aftab
    Commented Apr 26, 2017 at 11:23
  • This will cause problems if your theme or plugins try to create transients to cache things. You'll also need to make exceptions for the Admin AJAX endpoint which is used on the frontend
    – Tom J Nowell
    Commented Apr 26, 2017 at 13:12
  • Thanks @TomJNowell the site has minimal plugins and a custom theme but out of interest sake, are you saying that the front end may need to write to the database in the normal course of users simply viewing content? There is no functionality other than displaying posts - there isn't even a form.
    – Daniel
    Commented Apr 27, 2017 at 11:29

1 Answer 1


I have setup WP instance with 2 different database, one for read only and other for admin purpose.

But once the admin make the changes, then the ADMIN-DB should be copied and added to SLAVE-DB server.

// check if url contains wp-login or wp-admin and then create DB configuration for Master DB else Slave DB.
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if ( false !== strpos( $url, 'wp-admin' ) || false !== strpos( $url, 'wp-login' ) ) {
    define( 'DB_NAME', 'SLAVE_DB_NAME' );
    define( 'DB_USER', 'SLAVE_DB_USER' );
    define( 'DB_HOST', 'SLAVE_DB_HOST' );
} else {
    define( 'DB_NAME', 'MASTER_DB_NAME' );
    define( 'DB_USER', 'MASTER_DB_USER' );
    define( 'DB_HOST', 'MASTER_DB_HOST' );

  • Question was cool, so my answer helped you out :)
    – Aftab
    Commented Apr 26, 2017 at 11:43
  • Definitely, I realised after I asked it that I can use one DB and have the different users in the configs. Did you put this code in the wp-config?
    – Daniel
    Commented Apr 26, 2017 at 11:44
  • 1
    Yes, in wp-config.php file .
    – Aftab
    Commented Apr 26, 2017 at 11:44
  • Is it possible that your if statement has the DB's in the wrong order? When I tried this on my dev site it worked but I couldn't log in - I assumed this was somehow related to the user and how WP stores sessions in the DB so I altered it to if ( strpos( $url, 'wp-admin' ) || strpos( $url, 'wp-login' ) ) { and switched the DB's around which now works
    – Daniel
    Commented Apr 26, 2017 at 12:06
  • 1
    oops, yes the order is incorrect. thanks for correcting it
    – Aftab
    Commented Apr 26, 2017 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.