2

I am trying to choose different databases for admin and user to decrease the load on database. in wp-config.php when i am trying to use the code

if(is_admin())
{
define('DB_NAME', 'wp_db');
}
else
{
define('DB_NAME', 'other_wp_db');
}

both databases uses same user name and password.

I am getting an error of

Fatal error: Call to undefined function is_admin() in D:\xampp\htdocs\wordpress\wp-config.php on line 24

How to use is_admin() function in the wp-config.php file?

can someone please help.

4

I would be careful doing this, because you are assuming all your database modifications will only happen within the admin backend, but that might not always be the case. The wp-cron comes to mind, but there are also some plugins that use front-end writes. So you might get nasty sync problems with your two databases, using this method.

There are many things you can do before going the master/slave route:

  • Check for example your logs, if there's anything strange going on there.
  • Maybe it's a plugin that's giving you problems?
  • Maybe you have a WP_Query that's not efficient?
  • Maybe you can use the transients API?
  • Maybe you can consider persistant object cache?
  • There are many cache plugins out there that can help.
  • Maybe the hosting environment is slowing you down?
  • Maybe the MySQL server isn't tuned well enough? Some helpful tuning scripts exists.
  • Maybe it's time to update PHP, MySQL and start using NginX ;-)
  • ...

If you decide this route is necessary, there are already solutions out there, like the HyperDB plugin, that gives you a db.php drop-in to be placed in the wp-content directory. It checks for any writes/updates in the incoming SQL queries and redirects them to the master database. But I just scanned through the source and it looks like it's not supporting the mysqli extension, only the deprecated mysql. If you don't like forking this yourself, there seems to exists at least one fork that supports mysqli (I'm not related to this project).

| improve this answer | |
2

It's too early to use this WP built-in function. You can use it in some plugin or theme files, but not when WP enviroment is still loading.

As I am not really sure if your intention is really correct, you have to use some other way (with PHP native functions) to distinct between front-end and administration. For example something like this:

if( strpos($_SERVER['REQUEST_URI'],'/wp-admin/') !== FALSE ) {
  define('DB_NAME', 'wp_db');
} else { ... }

But I don't think it is bulletproof solution. Ajax call are processed in administration, wp-login.php is not covered by the code above, etc.

| improve this answer | |
-1

Try to insert your code

if(is_admin()) {...}

AFTER

require_once(ABSPATH . 'wp-settings.php');

because function is_admin() is loaded while wp-setting.php is executed. In this way you could avoid the error message, but I cannot understand why have your site to work correctly only for administrator? It seems the semantic error. I think some components will not work correctly under administrator too.

| improve this answer | |
  • there is an explicit comment in the code warning against inserting code there.... – Mark Kaplun Nov 24 '17 at 4:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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