0

I have a WP site and also have a external DB (means not WP DB) with users. The insertion process at that table uses the same algorithm as WP uses. What I'm need to do here is login in WP but using this external DB which is in the same host by the way. I check this links http://www.tyssendesign.com.au/articles/cms/connecting-to-external-database-from-within-wordpress/ and http://wordpress.org/extend/plugins/external-database-authentication/ and for the first is not what I'm want but for the second one is not compatible with WP 3+ and I'm using 3.3.1, any suggestion or docs or something else?

Widor
  • 13,003
  • 7
  • 42
  • 64
ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • Just because you are running Wordress, nothing is stopping you from using php and sql as usual. I suggest your write your custom php function that makes an SQL connection to your external database and queries it to authenticate the user. It will not break even if you update Wordress in the future. – anuragbh May 23 '12 at 16:52
  • @anuragbh I'm not a WP expert so I not know where I must write that function and how to use for the login proccess. Any docs or place to read about? Any example from you or tips? – ReynierPM May 23 '12 at 16:54
  • Whats the schema for your users table? You can create a process_login.php file and put your login code in there. It will receive the variables from the form that the user will submit with their username and password, and you can query it against the database to see if they match. Here is a very simple way: http://pastebin.com/vjfgpYdV It does not use salt or anything, but you can find more stuff here: http://stackoverflow.com/questions/685855/how-do-i-authenticate-a-user-in-php-mysql http://tebros.com/2010/01/secure-username-and-password-authentication-with-php/ – anuragbh May 23 '12 at 17:19
  • @anuragbh this is my schema http://pastebin.com/nH1WJkqF for the table in which I store username and password. Now I suppose must be define here the connection used to get this data from external database and so on but, how to redirect in the same way as WP does? also where I should put this page in WP and how to call it – ReynierPM May 23 '12 at 17:42
  • Connect using: `$link = mysql_connect('example.com:3307',mysql_user','mysql_passwd'); if (!$link) { die('Could not connect: ' . mysql_error()); }` This will open a second connection to a mysql database and keep the WP connection open too. It will not be a page, but just a script that gets called to process login. Put the login process file in your active theme folder. To redirect to another page, you can do this inside your process file: `` – anuragbh May 23 '12 at 17:54
  • You can change the query and column names as per your schema. – anuragbh May 23 '12 at 17:55
  • @anuragbh many thx and this should work I'm trying in this moment. Now for redirection Which are the pages that Wordpress use by default if login is successfully and if not? – ReynierPM May 23 '12 at 17:59
  • Wordpress takes the user to the dashboard if login is successful at: www.domain.com/wp-admin – anuragbh May 23 '12 at 18:12
  • @anuragbh ok aditional to that I must create sessions vars or take care about other things that could happen in the WP login process? Where I can find that function? – ReynierPM May 23 '12 at 18:17
  • I am not sure about how wordpress handles sessions or keeps track of logged in users. But since your users are not in Wordpress users table, you might need to make your own way. – anuragbh May 23 '12 at 18:25
  • @anuragbh the last question, can I include at top the file require( dirname(__FILE__) . '/wp-load.php' ); just like wp_login.php does and then use Wordpress native functions? Also please answer the question so I can give you the points and accept your help – ReynierPM May 23 '12 at 18:32
  • Yes you can use: require_once("/path.../wp-load.php"); and use native Wordpress functions in your custom PHP code. – anuragbh May 23 '12 at 18:44

3 Answers3

1

You can write your custom php script to connect to the external database that has your users table, and query the form fields submitted by the user against the table to authenticate.

You can use this as a reference to writing your php code: How do I authenticate a user in PHP / MySQL?

Community
  • 1
  • 1
anuragbh
  • 603
  • 1
  • 5
  • 11
0

Just make sure you have your database login info of the external database when you instal wordpress. You can also edit the wp-config.php file if you have already installed it. Then you may need to edit the baseurl in the wp database.

ZiggidyCreative
  • 335
  • 3
  • 16