1

I've a website currently running in CI and it has a user table. I'm making a WordPress website and need it to use the user table of the CI database for users to login in WordPress. How can I use only that table of different database? Both DBs are on same server. Also I have to differentiate between admin and normal users, as admins can enter the dashboard. I've tried this plugin but as soon as I activate this plugin my login page stops working and displays a 500 server error.

migrated from stackoverflow.com Jan 10 '16 at 18:30

This question came from our site for professional and enthusiast programmers.

1

It might work to use a view as essentially a table alias, by doing the something like the following with the WordPress instance mysql database

 DROP TABLE wp_users
 CREATE VIEW wp_users AS SELECT * FROM CI.users;

However

  • will probably need to tweak the view creation to map the columns
  • the password encoding may well be different, so you might have hack WP core anyway to handle this
  • you probably need to manually modify the wp_usermeta table to properly signify admins.
0

You don't need a plugin to make multiple db connections in CI. Just do this:

Connection 1 - /application/config/database.php

This is your default CI database config

$db['default']['hostname'] = 'DB_HOST';
$db['default']['username'] = 'DB_USER';
$db['default']['password'] = 'DB_PASSWORD';
$db['default']['database'] = 'DB_NAME';

$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

Connection 2 - /application/config/database.php

Duplicate that chunk of code, renaming the $db['default'] with something like $db['secondary']

$db['secondary']['hostname'] = 'SECONDARY_DB_HOST';
$db['secondary']['username'] = 'SECONDARY_DB_USER';
$db['secondary']['password'] = 'SECONDARY_DB_PASSWORD';
$db['secondary']['database'] = 'SECONDARY_DB_NAME';

$db['secondary']['dbdriver'] = 'mysql';
$db['secondary']['dbprefix'] = '';
$db['secondary']['pconnect'] = TRUE;
$db['secondary']['db_debug'] = TRUE;
$db['secondary']['cache_on'] = FALSE;
$db['secondary']['cachedir'] = '';
$db['secondary']['char_set'] = 'utf8';
$db['secondary']['dbcollat'] = 'utf8_general_ci';
$db['secondary']['swap_pre'] = '';
$db['secondary']['autoinit'] = TRUE;
$db['secondary']['stricton'] = FALSE;

Initiate and use Connection 2

function getWordpressUsers() {

    $secondaryDb = $this->load->database('secondary', TRUE);

    $query = $secondaryDb->select('user_email, display_name')->get('wp_users');
    var_dump($query);

}

For more info about it, check this out: https://codeigniter.com/user_guide/database/connecting.html

  • Thanks for your reply but this is for CI to use other DB, I want wordpress to use different DB for user to login. – Umair Malik Dec 30 '15 at 6:35
  • Sorry - wasn't clear from your question that this was the other way around. – Lionel Ritchie the Manatee Dec 30 '15 at 15:02
  • Have you looked at wordpress multisite installs, or is the database you want to use already existing elsewhere? Also, is there a possibility that you could update wp_users from the other engine and connect to the WP database with CI, instead of the other way around? – Lionel Ritchie the Manatee Dec 30 '15 at 15:04
0

It's not possible in Wordpress to have shared users between 2 separates installations on 2 separated databases.

It could be possible just by hardcoding:

  1. hardcode WP_User class connect to other database and retrieve users from there
  2. hardcode get_metadata() function, that for wp_usermeta table connect to other database and retrieve users from there

but mentioned solution is not recommended of course, because it's not updateproof.

Your Answer

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