Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am working on a custom function for login in my users via facebook.

I started by reading this nice tutorial on how to login in via facebook: http://digitalzoomstudio.net/2013/02/09/tutorial-how-to-login-via-facebook-in-your-wordpress-blog/

The function will log in the user to wordpress if an account with his/her facebook email already exists in the wordpress blog. If it doesnt exist, it will automatically create an account for the user.

Everything works as expected. However, I have an issue when login in and login out.

There is a "Login with Facebook" button. This button will change to Logout (this logout button is supposed to logout the user from facebook) if the user logged in.

One issue (LOGIN IN):

  • The first time the user clicks the "Login with facebook" button, the user is logged into the wordpress blog; however, Facebook API wont recognize the user thus making the facebook login button output a LOGIN button instead of a LOGOUT button.

  • The second time, the user clicks the LOGIN button, Facebook API DOES recognize the user and the button changes to LOGOUT.

So, why is it that the first time the user is not being recognized by facebook but the second time is?

The other issue is this (LOGIN OUT):

  • Once the user logs in the first time it hits the facebook LOGIN button, and wants to logout from my wordpress blog through a custom made logout button (this button is supposed to logout the user from the blog only and not from facebook), it works perfectly!

  • However, if the user has logged in by clicking the facebook LOGIN button a second time, the custom made logout button has to be clicked twice in order to logout the user successfully.

I know how to solve this partially. I could just check if the user is logged in the blog and remove the LOGIN button; however, I want to know why I am getting this strange behavior from facebook and how it is conflicting my blog.

UPDATE

For the second issue (when user logs out) I found the solution. I just had to destroy facebook session $facebook->destroySession();

UPDATE 2: SOLVED!

The tutorial from the link I provided was destroying the facebook session after login in and when creating a new account for some reason. I just had to remove $facebook->destroySession(); from both parts and now everything is working as intended.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The tutorial from the link I provided was destroying the facebook session after login in and creating a new account for some reason. I just had to remove $facebook->destroySession(); from both parts and now everything is working as intended.

Here is the code from the tutorial with both lines commented out.

if ($fbuser) {


    $fb_registerpage_name = __('Facebook Register', 'bloora');
    $fbpage = get_page_by_title( $fb_registerpage_name );

    if(isset($user_profile['email'])){
        $user_name = $user_profile['email'];
        $user_email = $user_profile['email'];
        $user_id = username_exists( $user_name );
        if ( !$user_id and email_exists($user_email) == false ) {
                $random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
                $user_id = wp_create_user( $user_name, $random_password, $user_email ); 
                wp_set_current_user( $user_id );
                wp_set_auth_cookie( $user_id );
               // $facebook->destroySession(); THIS HAS TO BE REMOVED

        } else {
                $random_password = __('User already exists.  Password inherited.');

                wp_set_current_user( $user_id );
                wp_set_auth_cookie( $user_id );
               // $facebook->destroySession(); THIS HAS TO BE REMOVED!
        }
    }else{

        if(!is_admin() && isset($_POST) && count($_POST) > 0){

            if($fbpage){

                echo '<script>if(window.location.href != "'.get_permalink($fbpage->ID).'"){ window.location.href = "'.get_permalink($fbpage->ID).'"; }</script>';
            }else{
                // Create post object
                $my_post = array(
                  'post_title'    => $fb_registerpage_name,
                  'post_content'  => 'This is my post.',
                  'post_status'   => 'publish',
                  'post_type'   => 'page',
                  'post_author'   => 1
                );

                // Insert the post into the database
                $fbpage = wp_insert_post( $my_post );
                echo '<script>if(window.location.href != "'.get_permalink($fbpage->ID).'"){ window.location.href = "'.get_permalink($fbpage->ID).'"; }</script>';

            }
        }
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.