WordPress.org

Ready to get started?Download WordPress

Forums

Simple Facebook Connect
Using FB.api with the plugin (5 posts)

  1. jk_
    Member
    Posted 2 years ago #

    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.

    $(document).ready(function(){
    
    FB.api('/me/likes/40796308305',function(response) {
    	if( response.data ) {
    		if( !isEmpty(response.data) )
    			alert('You are a fan!');
    		else
    			alert('Not a fan!');
    	} else {
    		alert('ERROR!');
    	}
    });
    
    // function to check for an empty object
    function isEmpty(obj) {
    	for(var prop in obj) {
    		if(obj.hasOwnProperty(prop))
    			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 }
    add_action('wp_footer','add_facebook_doyoulike',30);

    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.

    Jk_

  2. Samuel Wood (Otto)
    Tech Ninja
    Plugin Author

    Posted 2 years ago #

    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:

    add_action('sfc_async_init','my_js_code');
    function my_js_code() {
    ?>
    javascriptfunction();
    <?php
    }

    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.

  3. Samuel Wood (Otto)
    Tech Ninja
    Plugin Author

    Posted 2 years ago #

    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.

    <?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.

  4. alebai60
    Member
    Posted 2 years ago #

    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"

  5. Samuel Wood (Otto)
    Tech Ninja
    Plugin Author

    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic