LOGIN IN WORDPRESS FROM OUR OWN WEBSITE

Now a day, a lot many people are login in wordpress as a blog for their main website. Though this is a nice idea to use the power of wordpress for your main blog but it comes with a limitation.

How would you integrate the sessions of your site and wordpress??

that is, if user log in wordpress from our own website, he shall be auto logged in to the wordpress blog as well. Cool no!! By this, we can use the wordpress to the fullest without worrying for a possible annoyed user due to two logins (one on site and one on wordpress)

Now, how would you do this?? How to integrate the session of wordpress with your own site?

for this you need to register the user in both wordpress table and in your website registration table when the user registers for the first time or if you can manage your website`s registration table from wordpress table then it will be very good and easy for making queries.

IT WILL NEED ONLY THE SESSION VARIABLES WHICH WE ARE USING IN OUR WEBSITE FOR CREATING SESSION.

So for this we need to customize our WordPress theme`s functions.php (WP ROOT > wp_content > your_theme > functions.php)

we have to create a function


add_action( 'wp_head', 'wp_session_website_head' );
function wp_session_website_head() {
global $wpdb;
if( current_user->data->ID) {
if(!empty ($_SESSION['User_id'])) {
$fetchidquery = "Select ID from $wpdb->users where user_email
= '".$_SESSION['E-mail']."'";
$thisuserid = $wpdb->get_row($fetchidquery);
wp_set_current_user( $thisuserid->ID,'');
$user_id = $thisuserid->ID;
}

Here add_action will add this function into theme’s header.php which will check the website session which is defined as $_SESSION[‘User_id’] and if it is already existed then it will fire a query from users table to fetch all the details of that user id which is present in the session variable

  • If wordpress session is not created in some pages then include the mentioned below line

wp_header();

  • The User_id & E-mail variable used in session depend upon the variable used in your coding while creating session. So change it accordingly.

    Thank You !!!!

Now, even you can consider placing an advertisement on the site. It could be money well spent as your advert will appear in front of a highly-targeted audience. What we have discussed so far is not a big deal to handle but commitment and endemic devotion need to be given to have the best results. Thus, you need to put your inputs regularly in different ways to keep pace in the development of your page and then only, you will be ensured of something worth done.