Support » Plugin: Facebook » global $facebook variable not accessible for php api calls

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Niall Kennedy

    (@niallkennedy)

    How are you using the $facebook global? Is it possible you have other code overriding the variable?

    The Facebook plugin code uses something similar to the following quite frequently:

    global $facebook;
    if ( ! isset($facebook) ) return
    $facebook->api( '/me' )

    Thanks for a prompt reply…

    Yeah it was all fine till recent spate of updates from Facebook Social Plugin, which is leaving a bad taste. I also use similar code at various places on my site. Take for example the following piece of code:

    global $facebook;
    if ( isset($facebook) )
    {
    //$url = get_permalink($post->ID);
    
    $newurl='http://preetisblog.com/programming/display-combined-comments-count-of-facebook-and-wordpress-comments-on-blog/';
    $myuser_profile = $facebook->api('/?ids='.$newurl,'GET');
           // echo "Shares: " . $myuser_profile[$newurl]['shares']."\n";
    //echo "Comments: " . $myuser_profile[$newurl]['comments']."\n";
    $fbcount=$myuser_profile[$newurl]['comments'];
    echo "count=$fbcount";
    }
    else
    {
    echo "no facebook";
    }

    This works well at a normal php page like here, which does not has facebook plugin installed.

    But does not works here (see on righthand side-bar bottom), where you see “no facebook” text. Further if i try to declare global $facebook as per php sdk by using configuration values again, it results in failure of all facebook plugin components on site like the comments box, subscribe button, like button etc..

    Thus m not able to make any api calls via php sdk…

    Plugin Author Niall Kennedy

    (@niallkennedy)

    The Facebook PHP SDK is loaded by the plugin in an admin context only. The variable is set during the init action.

    see Facebook_Loader->load_php_sdk()

    Thanks Niall for that input.

    Thus could finally figure out the correct way to use global $facebook post these updates is as shown by the following code:

    global $facebook;
    if (! ( isset($facebook) ))
    {
       global $facebook_loader;
       if(!(isset($facebook_loader)))
       {
         $facebook_loader = new Facebook_Loader();
       }
      if (! ( isset($facebook) ))
       {
        $facebook_loader->load_php_sdk();
       }
    }
    if ( isset($facebook) )
    { ... do whatever you want with $facebook after this....

    may be becoz the load_php_sdk function is inside Facebook_Loader class. May be if my approach is correct, we can get it documented in the plugin for correct usage by everyone…

    Changing the status to resolved, as no changes required in plugin except for the documentation part…

    Regards

    Plugin Author Niall Kennedy

    (@niallkennedy)

    The plugin loads during the init action at default priority (10). If you are adding checks for these variables you’ll want to be sure to check later in the WordPress loading process than init 10.

    I would check Facebook_Loader exists before trying to instantiate.

    Yeah you are right Niall, checking Facebook_Loader first will be in order. Can u explain to me how I can force my code to run after the plugin has done its job?

    Plugin Author Niall Kennedy

    (@niallkennedy)

    The main plugin code runs during the init action at priority 0. If you attach your own code to the init action after 0 you should be able to interpret initialized variables.

    Example: add_action( ‘init’, ‘your_function_name’, 1 );

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘global $facebook variable not accessible for php api calls’ is closed to new replies.