Skip to content

Instantly share code, notes, and snippets.

@joshapgar
Created November 3, 2017 19:29
Show Gist options
  • Save joshapgar/5bd86cc8ae1c9fc0efe88968a29fa071 to your computer and use it in GitHub Desktop.
Save joshapgar/5bd86cc8ae1c9fc0efe88968a29fa071 to your computer and use it in GitHub Desktop.
Authenticate with Facebook Button
public function authenticate_with_facebook() {
?>
<p><a href="#" onClick="logInWithFacebook()">Log In with the JavaScript SDK</a></p>
<script>
logInWithFacebook = function() {
FB.login(function(response) {
if (response.authResponse) {
//alert('You are logged in &amp; cookie set!');
// Now you can redirect the user or do an AJAX request to
// a PHP script that grabs the signed request from the cookie.
jQuery.ajax({
url: '<?php echo plugin_dir_url(__FILE__); ?>facebook-js-login.php',
type: 'POST',
data: {
app_id: "<?php echo get_option('facebook_app_id'); ?>",
app_secret: "<?php echo get_option('facebook_app_secret'); ?>"
}
}).done(function(data){
//alert(JSON.stringify(data));
});
} else {
//alert('User cancelled login or did not fully authorize.');
}
}, {scope: 'public_profile, email, user_likes, user_photos, user_posts, user_events'});
return false;
};
window.fbAsyncInit = function() {
FB.init({
appId: <?php echo get_option('facebook_app_id'); ?>,
cookie: true, // This is important, it's not enabled by default
version: 'v2.2'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment