Step 1. Obtaining Application ID and App secret
Yes, first of all you have to create FaceBook application. You will find ready-to-use instructions in my another post, that’s it. When you obtain both Application ID and Secret, please come back here.

Step 2. Configuring App Domains

When you work with FaceBook Oauth, it is important to configure domains properly. You can do it in just created app as well.

  • go to your application basic settings and specify your website domain in App Domains field,
  • then click the Add Platform button, choose Website and specify your website domain there as well,
  • save changes.

Step 3. Custom Page Template
The simplest way is to use WordPress custom page templates. Just create a file in your current theme folder and paste the following code in it.

Php Code Below

/*
 * Template name: Custom Login
 */
 
$client_id = ''; // Facebook APP Client ID
$client_secret = ''; // Facebook APP Client secret
$redirect_uri = ''; // URL of page/file that processes a request
 
// in our case we ask facebook to redirect to the same page, because processing code is also here
// processing code
if ( isset( $_GET['code'] ) && $_GET['code'] ) {
 
    // first of all we should receive access token by the given code
    $params = array(
        'client_id'     => $client_id,
        'redirect_uri'  => $redirect_uri,
        'client_secret' => $client_secret,
        'code'          => $_GET['code'] 
    );
 
    // connect Facebook Grapth API using WordPress HTTP API
    $tokenresponse = wp_remote_get( '' . http_build_query( $params ) );
 
    $token = json_decode( wp_remote_retrieve_body( $tokenresponse ) );
 
    if ( isset( $token->access_token )) {
 
        // now using the access token we can receive informarion about user
        $params = array(
            'access_token'    => $token->access_token,
            'fields'        => 'id,name,email,picture,link,locale,first_name,last_name' // info to get
        );
 
        // connect Facebook Grapth API using WordPress HTTP API
        $useresponse = wp_remote_get('' . '?' . urldecode( http_build_query( $params ) ) );
 
        $fb_user = json_decode( wp_remote_retrieve_body( $useresponse ) );
 
        // if ID and email exist, we can try to create new WordPress user or authorize if he is already registered
        if ( isset( $fb_user->id ) && isset( $fb_user->email ) ) {
 
            // if no user with this email, create him
            if( !email_exists( $fb_user->email ) ) {
 
                $userdata = array(
                    'user_login'  =>  $fb_user->email,
                    'user_pass'   =>  wp_generate_password(), // random password, you can also send a notification to new users, so they could set a password themselves
                    'user_email' => $fb_user->email,
                    'first_name' => $fb_user->first_name,
                    'last_name' => $fb_user->last_name
                );
                $user_id = wp_insert_user( $userdata );
 
                update_user_meta( $user_id, 'facebook', $fb_user->link );
 
            } else {
                // user exists, so we need just get his ID
                $user = get_user_by( 'email', $fb_user->email );
                $user_id = $user->ID;
            }
 
            // authorize the user and redirect him to admin area
            if( $user_id ) {
                wp_set_auth_cookie( $user_id, true );
                wp_redirect( admin_url() );
                exit;
            }
 
        }
 
    }
}
?>




    
    Facebook Login

 

Login via Facebook

 

 

 

Find Us On

icon 01

Where We Are

New Delhi Dwarka Mor 110059

icon 02

10am To 5pm Tech Support

(+91) 7503132088

(+91) 8700584697

This website uses cookies. By continuing to browse the site, you are agreeing to our use of cookies
X
-->