$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to use user table of a different database for WordPress users?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to use user table of a different database for WordPress users?

matteradmin9PV0评论

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.

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.

Share Improve this question edited Jan 29, 2018 at 20:44 Glorfindel 6113 gold badges10 silver badges18 bronze badges asked Dec 29, 2015 at 13:43 Umair MalikUmair Malik
Add a comment  | 

3 Answers 3

Reset to default 2

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.

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/user_guide/database/connecting.html

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.

Post a comment

comment list (0)

  1. No comments so far