在Wordpress中集成Facebook JavaScript SDK

时间:2016-08-30 11:58:47

标签: javascript php wordpress facebook-graph-api facebook-javascript-sdk

我想要实现的是我想知道某个用户是否使用我的插件登录facebook。

我所做的是按照facebook的说明进行操作。

在wordpress的标题中的<script> window.fbAsyncInit = function() { FB.init({ appId : 'my_app_id', xfbml : true, version : 'v2.7' }); }; (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> 下方,我添加了

    <script type="text/javascript">

    FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    // the user is logged in and has authenticated your
    // app, and response.authResponse supplies
    // the user's ID, a valid access token, a signed
    // request, and the time the access token 
    // and signed request each expire

    var uid = response.authResponse.userID;
    var accessToken = response.authResponse.accessToken;

    document.write('connected');
  } else if (response.status === 'not_authorized') {
    // the user is logged in to Facebook, 
    // but has not authenticated your app
    document.write('not_authorized');
  } else {
    // the user isn't logged in to Facebook.
    document.write('not connected');
    }
 });


</script>

我在那里添加了我的App ID

在插件中,我使用上面的教程来了解如何识别用户。所以我将这段代码添加到插件

static
  

但问题是它不起作用。没有显示任何内容。

我做对了吗?或者我错过了什么?

1 个答案:

答案 0 :(得分:0)

想想你应该在facebook上显示Facebook登录按钮。例如:

<div class="fb-status text-center">
    <fb:login-button data-size="xlarge" scope="public_profile,email,user_photos">
    </fb:login-button>

    <div id="status">
    </div>

然后以某种方式你需要知道用户尝试使用facebook登录,然后检查用户是否允许使用他在域中定义的名称,电子邮件和其他选项。

在我的插件中是这样的。我知道这是不好的做法,但它对我有用。

<script>
    fbGalleries.loginStatus = false;

    function checkLoginState() {
            FB.getLoginStatus(function(response) {
                statusChangeCallback(response);
            });
            if (fbGalleries.loginStatus == false) {
                setTimeout(checkLoginState, 2000);
            }
            else {
                $('.fb-status').remove();
            }
        }

      window.fbAsyncInit = function() {
            FB.init({
                appId      : '*****************',
                cookie     : true,  // enable cookies to allow the server to access
                                    // the session
                xfbml      : true,  // parse social plugins on this page
                version    : 'v2.5' // use graph api version 2.5
            });

        };

        // Load the SDK asynchronously
        (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'));

    function statusChangeCallback(response) {
        if (response.status === 'connected') {
            testAPI();
        } else if (response.status === 'not_authorized') {
            document.getElementById('status').innerHTML = 'Please log ' +
                'into this app.';
        } else {
            document.getElementById('status').innerHTML = 'Please log ' +
                'into Facebook.';
        }
    }

    function testAPI() {
        FB.api('/me', function(response) {
            console.log('Successful login for: ' + response.name);
            document.getElementById('status').innerHTML =
                'Thanks for logging in, ' + response.name + '!';
            getFbPhotos(); //in my case pooling user photo galeries
            fbGalleries.loginStatus = true;
        });
    }

</script>

您需要运行的地方checkLoginState();