I have 2 wordpress sites on the same hosting and the same host/user/ password for the database I have on the other hand 2 different database and 2 different prefixes I need, during an action on site1, to go and make requests on site2 (SELECT and after, UPDATE) I saw that we could do

$mydb = new wpdb('username','password','database','localhost');
$rows = $mydb->get_results("select Name from my_table");

But putting that in site1's functions.php file isn't very secure is it?

How to do this in a clean way and avoid any attempt to hack the database if you access the functions.php file


1 Answer 1


Putting an important credentials in functions.php is not a good idea. The better way is you can utilize WP REST API from your second site to handle GET and POST method for the data and using global $wpdb variable inside the code.

You can either create a custom endpoint for that by making a custom plugin or insert the code into functions.php. You may need to configure the permission_callback parameter for the API endpoint whether it is being used internally or publicly.

Read more on REST API Handbook

  • I succeeded by adding the information in the wp-config file (eg SECOND_USER_DB etc) that's fine too isn't it? Commented Dec 9, 2020 at 9:44

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.