Support » Plugin: Simple Facebook Connect » [Plugin: Simple Facebook Connect] Using FB.api with the plugin

  • Hi there,

    I’m trying to use the Graph Api with the init made by Simple Facebook Connect. However, it returns an annoying “FB is not defined” message !

    This call checks if the user LIKE the page with a specific ID.

    FB.api('/me/likes/40796308305',function(response) {
    	if( ) {
    		if( !isEmpty( )
    			alert('You are a fan!');
    			alert('Not a fan!');
    	} else {
    // function to check for an empty object
    function isEmpty(obj) {
    	for(var prop in obj) {
    			return false;
    	return true;

    I load my script after the FB.init (which is placed just before the </body> tag). Any clue on how I could solve this ?

    function add_facebook_doyoulike()
    {  ?>
     <script src="<?php echo get_template_directory_uri(); ?>/js/doyoulike.js"></script>  
    <?php }

    Facebook advice to load the JS SDK just after the <body> tag, could it be the problem here ?

    The following code will load and initialize the JavaScript SDK with all common options. Replace YOUR_APP_ID and http://WWW.YOUR_DOMAIN.COM with the appropriate values. The best place to put this code is right after the opening <body> tag.

    Thanks in advance.


Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Samuel Wood (Otto)

    (@otto42) Admin

    SFC loads the Facebook Javascript code asynchronously (using example code from FB’s site), to avoid slowdown on page loads. This means that FB hasn’t loaded and init’d yet at the document-ready point.

    So, to hook your own JS code in which is dependent upon FB’s code, you have to run your code after the main init action has actually taken place already, or you won’t be able to access the FB code. I left a hook there just for that purpose.

    The sfc_async_init action hook can be used to output JS code to run immediately after the FB.init action. You can use it like this:

    function my_js_code() {

    Basically, put your own JS code into a function somewhere, then have it get called at this point in time. This will then run after FB is init’d and your code can be called safely.

    If you’re interested, the way the code I have in there works is that a function is defined and set to the window.fbAsyncInit. Another function creates the script tag to load the FB all.js file, then inserts it into the DOM at the fb-root point. When all.js runs, it executes the code in window.fbAsyncInit, which contains the FB.init call, and whatever is output from the action hook. Thus, even though your code in the footer is coming after the FB.init call in the HTML, it is still running too early because that FB.init call doesn’t happen until all.js has loaded up asynchronously.

    Note that you don’t need to use the .ready() call with this approach. Just a normal JS function containing your FB-dependent code, then a call to that function on the sfc_async_init action hook. The code will be called at the proper time, and that will be well after the document-ready point.

    Plugin Author Samuel Wood (Otto)

    (@otto42) Admin

    BTW, the sfc_is_fan() function will check if the user likes your page or not too. You don’t necessarily need Javascript for this, as it’s built into sfc-base.php.

    if (sfc_is_fan()) echo "user is a fan";
    else echo "user is not a fan";

    For this to work, you will have had to setup auto-publishing at some point though. The auto-publisher can be disabled, but the access_tokens have to be valid for it to make the request.



    When adding a FB.api call using /me/ I get error:

    “An active access token must be used to query information about the current user.”

    Error Code: “2500”

    Plugin Author Samuel Wood (Otto)

    (@otto42) Admin

    Right, which means that you need to add the access_token for a user to the call. Or not use /me/. Either way.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Plugin: Simple Facebook Connect] Using FB.api with the plugin’ is closed to new replies.