Sometimes you have two different websites powered by WordPress running on your server, one for your main features and other is for blogging. Main website has some users but you do not want the same users to register again for your blog also. When the user will login your website, it will automatically be logged in to your blog also.
To achieve this, you have to follow some basic criteria –
1. At first, install the blog on same database with another table prefix.
2. Make sure, you define same DB_NAME, DB_USER, DB_PASSWORD, DB_HOST and authentication keys except the table prefix. For quick result, just copy the main website wp-config.php file and overwrite the wp-config.php file on blog website ( *** Don’t forget to change the table prefix ).
3. Now, to access the main website users table, you have to define some variables in your blog’s wp-config.php file. Write the code in your wp-config.php file –
define(CUSTOM_USER_TABLE,'wp4_users'); // Change 'wp4' to match the prefix set during define(CUSTOM_USER_META_TABLE,'wp4_usermeta'); // Main Site's usermata table
Now, try to install the blog. You will see the below screenshots from WordPress –
4. After defining and setting tables access, now set COOKIE_DOMAIN and COOKIE_PATH same as main website in blog’s wp-config.php file.
define( 'COOKIEPATH', '/' );
The above code will try access main site’s cookie and session. But both web sites should have same COOKIE_HASH to work perfect. Define the below code in both wp-config.php files.
define( 'COOKIEHASH', md5( 'random_codes' ) ); // Write anything in md5 hash
Now, try to login in your main website and you will be automatically logged in your blog also.
Spoiler Alert: If nothing happens, try to clear all the cookies from your browser and try again. Your comment is welcome if the above is working or not.