6

Actually I have a php website in my hosting. Now I am personalizing a Wordpress theme to replace it. It's all ok for now, but the old website has a built in function to connect to another database with a token. This connection is only to get some data.

Can you tell me how to implement this in my new wordpress theme?
Did a wordpress plugin exists for this?

Thanks

5
  • Gracias a ti. Thanks. Commented Jun 17, 2016 at 16:51
  • what database? where? WP is nailed at the hips to mysql, generally, and won't know how to talk to a "foreign" db, especially if the old site is using vendor extensions in the sql syntax. Commented Jun 17, 2016 at 16:53
  • @MarcB If you look at the tags it's Mysql related database. With this trick you can use any query and work on it with php. I have done this before many times. Commented Jun 17, 2016 at 17:02
  • @LoicTheAztec: doesn't mean much. WP implies mysql, but "external database" DOESN'T Commented Jun 17, 2016 at 17:05
  • @MarcB Ok if you say it, then… The OP is satisfy with my answer, for his needs. That is the main purpose here. You can use WP API for all other cases or build a special plugin. Commented Jun 17, 2016 at 17:06

1 Answer 1

7

The easiest way to connect wordpress to a second database (without a token) is to add this little piece of code (replacing strings by your connexion data) :

$wpdb2 = new WPDB( 'remote_user', 'remote_password', 'remote_db', 'remote_server');

Then you can use this new object to query anything that you want to, just like the main $wpdb object. You can use this on the function.php file of your active child theme (or theme).

I think that maybe there is some plugins, for this kind of purpose… You will have to search.

Reference: Using wpdb to connect to a separate database

Sign up to request clarification or add additional context in comments.

2 Comments

Does this work with a sqlserver database? Or only with mySQL?
@BennettForkner This works for wordpress and all databases that wordpress allow.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.