Make website login work on WordPress too Make website login work on WordPress too wordpress wordpress

Make website login work on WordPress too


You can set the wordpress login to use a custom table by editing the config.php and adding these two lines:

define('CUSTOM_USER_TABLE','new_user_table'); //login, pass, email etcdefine('CUSTOM_USER_META_TABLE', 'new_usermeta_table'); //optional bio, don't have to include this line

Where new_user_table is your website's table and new_usermeta_table is your website's bio table (if you want one)

The custom table needs to have the same structure as a normal wordpress table. So, to get this working with your existing website's table you'll have to add some fields and make sure the password is hashed the same way.

Here is how to structure the user table

Here is how to structure the user meta table

To hash the passwords correctly at registration, include the file wp-includes/pluggable.php and use the function
<?php $hash = wp_hash_password( $password ) ?>

For existing passwords that are not hashed correctly, you'll have to set up an email password reset.

Or. if you'd like to retain your current password hashes (not recommended for security reasons but doable) you can change the wordpress hashing function. In wp-includes/pluggable.php change:

if ( !function_exists('wp_hash_password') ){    function wp_hash_password($password) {                //apply your own hashing structure here            return $password;    }}

And change:

if ( !function_exists('wp_check_password') ){    function wp_check_password($password, $hash, $user_id = '') {            //check for your hash match            return apply_filters('check_password', $check, $password, $hash, $user_id);            }}

For details on wp_check_password Go Here

Alternatively

You can skip messing around with your custom user table and have the wordpress login apply to the rest of your site. To do this, simple use the following code:

<?phpinclude 'wp-config.php';if ( is_user_logged_in() ) {    echo 'Welcome, registered user!';} else {    header( 'Location: http://google.com' ) ;};?>

Make sure that 'wp-config.php' is the full relative path to the file, then place this code in every page on your non wordpress site. replace the echo with whatever content is to be displayed for a logged in user, and replace the header with whatever is to be displayed for a guest. If the content is simple html you can do the following:

<?phpinclude 'wp-config.php';if ( is_user_logged_in() ) {?><html><head></head><body><p>Welcome Registered user</p></body></html><?php} else {?><html><head></head><body><p>Please log in</p></body></html><?php};?>


Try changing the options in your wp-config.php file to point to your members database; it's probably a good idea to create a separate user/password with MySQL and grant it privileges to your members DB. There a number of useful options you're able to set/change with that file. I suggest you read the documentation on it for some more in-depth stuff. If you do decide to try the above-mentioned approach, make sure to also change the $table-prefix line to match your existing DB.

Also, see if this blog post is of any help, as it deals with external authentication directly. There's a php script available which you can copy/paste or otherwise adjust to your particular needs. Pay particular attention to the include_once("../wp-config.php"); and include_once("../wp-includes/class-phpass.php"); lines.


I've managed to do that a while ago by using a function from the Wordpress API.

wp_set_auth_cookie( $wordpress_user_id, $remember, false );

$wordpress_user_id is the ID of the user in the Wordpress table.

$remember is a Boolean variable indicating whether Wordpress should create a persisting cookie "remembering" the user between sessions.

I don't remember the third one, but you could Google it easily.


P.S. To have the wp_set_auth_cookie function available you need to include/require some Wordpress files before you use it. Try with the wp-load.php file.