Facebook PHP SDK and WordPress Error

I have a developer environment setup with WAMP, WordPress, and PHPEdit IDE. I use the Facebook, Twitter, and YouTube API’s in a sidebar. I’m using Facebook’s PHP SDK to display information(no login or admin functions). Since the FB SDK and WP use session_start() I get the following warning:

Warning: session_start() [function.session-start]: Cannot send session
cache limiter – headers already sent (output started at
C:wampwwwdfiwp-contentthemesDFIheader.php:12) in
C:wampwwwdfiwp-contentthemesDFIapifacebook.php on line 36

Read More

I’m trying to figure this out by using the warning output but it doesn’t help considering the following. I know about clearing white space and characters before and after <?php ?> and placing session_start() before any http output. I use unix line enders and UTF8 encoding without BOM. My host server is not set up for output_buffering.

header.php line 11 to 13

11 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
12 <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes();?>>
13 <head>

It looks like the warning comes from inline php code. I don’t know what I can do to fix this line.

facebook.php line 34 to 37

34    public function __construct($config) {
35    if (!session_id()) {
36      session_start();
37    }

I don’t think I can stop either FB or WP from calling session_start() without breaking everything. How do I make WordPress and Facebook play nicely together without this error?

EDIT:
To stop the warning from displaying I put @ in front of session_start().

public function __construct($config) {
    if (!session_id()) {
      @session_start();
    }

Its only a workaround and I would still like to find the root of the problem.

Related posts

Leave a Reply

2 comments

  1. As discovered in your comments, the issue isn’t about including the PHP file, rather it is where you define the class. Creating the instance of the Facebook class can be safely done (as far as I know, it works for me) in the wp hook. This will allow you to define the instance of the class before any HTML output, then you can use that variable anywhere in your class.

    You do however want to be sure to only include the class once, but you can instantiate the class as many times as you want.

    Here’s a basic example to get you started:

    if( !class_exists( 'Facebook' ) ) {
        require_once 'facebook.php';
    }
    
    if( !class_exists( 'YourClass' ) ) {
    
        class YourClass {
    
            public $facebook = null;
    
            public function __construct() {
    
                add_action( 'wp', array( $this, 'define_facebook' ) );
                add_action( 'any_hook_after_wp', array( $this, 'example_usage' ) );
    
            }
    
            public function define_facebook() {
                global $post;
    
                // Assuming you are using post meta for the app ID and secret, you can use other methods though
                $app_id = get_post_meta( $post->ID, 'appId', true );
                $app_secret = get_post_meta( $post->ID, 'appSecret', true );
    
                $this->facebook = new Facebook( array( 'appId' => $app_id, 'secret' => $app_secret ) );
    
            }
    
            public function example_usage() {
    
                if( !is_null( $this->facebook ) ) {
    
                    // Lets see what we have here..
                    echo "<pre>";
                    print_r( $this->facebook );
                    echo "</pre>";
                    exit;
    
                }
    
            }
    
        }
    
    }
    
  2. You can using Hook Action init to check session_id exist or not.

    
    // add it into functions.php in theme folder
    add_action('init', 'themename_wp_session_start');
    function themename_wp_session_start(){
        if( !session_id() ){
            session_start();
        }
    }