views:

46

answers:

4

Greetings Everyone,

I'm currently using Wordpress as my CMS and leveraging Facebook's Graph API for the user account/registration "stuff".

My question is how do I use Facebook's PHP SDK (found here: http://github.com/facebook/php-sdk/), when Wordpress compiles several php files to output a single webpage? In other words, Wordpress' templates header.php + index.php + footer.php = what you see on www.example.com

If I create my instance of the Facebook Application in HEADER.PHP, how will I make references to them in INDEX.PHP and FOOTER.PHP?

Thanks in advance!

----- SAMPLE CODE ---

in header.php

<?php
require '/facebook.php';

// Create our Application instance.
$facebook = new Facebook(array(
  'appId' => 'xxx',
  'secret' => 'xxx',
  'cookie' => true,
));
?>

in index.php

<?php
$session = $facebook->getSession();
?>
+1  A: 

Once you include the facebook client in your header.php file and create an instance of that:

$facebook = new Facebook('API', 'SECRET');

You can then access the facebook client instance $facebook from your index.php file or even footer.php file or anyother file on which header.php is included.

Sarfraz
You sure? This is what I get after initiating in header and then having stuff call in index.phpFatal error: Call to a member function getSession() on a non-object in
st4ck0v3rfl0w
@st4ck0v3rfl0w:You should also post your sample code to see how you are actually doing it. It could help us further.
Sarfraz
Of course, good point sAc, see my edits
st4ck0v3rfl0w
+1  A: 

PHP is processed in order, that is you need to make sure that header.php is included before the call happens in index.php. It's possible that overall you have something like this:

In index.php

<?php
// blah blah other code
$session = $facebook->getSession();
// blah blah more code
include('header.php');

Which of course would be read as:

<?php
// blah blah other code
$session = $facebook->getSession();
// blah blah more code
require '/facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
  'appId' => 'xxx',
  'secret' => 'xxx',
  'cookie' => true,
));

You can see when you look at the overall code that $facebook wouldn't exist yet.

Add a var_dump($facebook); to the end of header.php (after you have set $facebook and also one to the start of index.php (right before you try to use $facebook).

The other thing that it might be is that you call to the Facebook Graph API isn't working correctly and is returning false or something. Your var_dump output will be the same if $facebook exists and is hasn't connected properly (or something like that), but will be different if you try to access $facebook before you have set it (one will be NULL and will generate an E_NOTICE, the other will be a Facebook object - or whatever is returned from the Facebook Graph API call).

Blair McMillan
You're absolutely right...ugh
st4ck0v3rfl0w
Actually....I just performed vardump in header.php when I created the $facebook instance in index.php and it returned NULL. I also did it the other way around and performed vardump in index.php when I created the instance in header.php and it also returned NULL.....WHAT'S GOING ON!?
st4ck0v3rfl0w
`var_dump($facebook)` returns `null` or `var_dump($facebook->getSession())` returns `null`? If it's the former, then you aren't connecting to Facebook properly, if it's the latter, it's because you don't have a Facebook session, make sure you have a "Connect to Facebook" button or something similar.
Blair McMillan
+3  A: 

If you set $facebook in header.php, you won't be able to access it in index.php unless you globalise it.

This is because header.php is loaded in via the function get_header(), which narrows the scope of all non-global variables.

So you could either globalise $facebook OR, since you want a single instance of Facebook, I'd strongly recommend using a singleton helper function;

function get_facebook_instance()
{
    static $f;
    if (!isset($f)) {
        if (!class_exists('Facebook'))
            require('/facebook.php');
        $f = new Facebook(array(
            'appId' => 'xxx',
            'secret' => 'xxx',
            'cookie' => true,
        ));
    }
    return $f;
}

Note: If you're using PHP5, you don't have to worry about returning by reference.

TheDeadMedic
Thank you @TheDeadMedic. You're right on. I placed the instance helper function inside functions.php and can now access the variables.
st4ck0v3rfl0w
Nice answer @TheDeadMedic and great link to boot :)
hsatterwhite
A: 

Consider using the Wordpress Facebook Connect Plugin, which already implements this.

Yuliy