WordPress.org

Ready to get started?Download WordPress

Forums

Facebook
[resolved] global $facebook variable not accessible for php api calls (8 posts)

  1. preetisblog
    Member
    Posted 1 year ago #

    global $facebook variable is not accessible for php api calls after the recent updates beyond version 1.02. This prevents all api calls from the site and thus no access to the graph api. I use php api for all this. till previous versions it was all smooth. Now even if i try to delare global $facebook, complete facebook plugin functions go hay wire.

    Request to look into the matter urgently..
    Regards

    http://wordpress.org/extend/plugins/facebook/

  2. Niall Kennedy
    Member
    Plugin Author

    Posted 1 year ago #

    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' )

  3. preetisblog
    Member
    Posted 1 year ago #

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

  4. Niall Kennedy
    Member
    Plugin Author

    Posted 1 year ago #

    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()

  5. preetisblog
    Member
    Posted 1 year ago #

    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

  6. Niall Kennedy
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  7. preetisblog
    Member
    Posted 1 year ago #

    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?

  8. Niall Kennedy
    Member
    Plugin Author

    Posted 1 year ago #

    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 );

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic