title: Wordpress and Facebook SDK for PHP / getSessionFromRedirect()
body: <p>I am trying to implement Facebook login into my Wordpress site, but <strong><code>$helper-&gt;getSessionFromRedirect();</code></strong> always returns NULL after being redirected from Facebook.</p>
  
  <p>My site has a custom Wordpress theme and in my '<code>header.php</code>' I include a file named '<code>login.php</code>' (which contains the Facebook SDK for PHP includes, the login-url, etc.) using the require_once() method.</p>
  
  <p>Everything works just fine when I copy the same code from the 'login.php' file into an external non Wordpress file (and change the paths to the includes not using Wordpress constant <strong>WP_CONTENT_DIR</strong> of course) and access it directly in my browser. The same code, however, included in my '<code>header.php</code>' will not work, whatever I try.</p>
  
  <p>It will simply not log me in as it can't create a session.</p>
  
  <p>So simplified, my script works fine, but not as part of my Wordpress theme. <strong>Any idea what could possibly be the cause that Wordpress makes this fail?</strong></p>
  
  <ul>
  <li>All settings in my Facebook app have been verified, including the trailing slash and the 'Valid OAuth redirect URIs.'</li>
  <li>When clicking the login-url in Wordpress, a session '<code>FBRLH_state</code>' is created, but not the required '<code>fb_token</code>.'</li>
  <li>When logged in from the external file, then visiting (not redirected to) the Wordpress theme, a session '<code>fb_token</code>' exists and the Facebook user is logged in as expected.</li>
  <li>Redirecting Facebook from the Wordpress theme to the external file (and visa versa) also doesn't work.</li>
  </ul>
  
  <p>Edit: Here is the code snippet:</p>
  
  <pre><code>define( 'FACEBOOK_SDK_V4_SRC_DIR', '/path/to/facebook-php-sdk/src/Facebook/' );
  require( '/path/to/facebook-php-sdk/autoload.php' );
  
  use Facebook\FacebookSession;
  use Facebook\FacebookRequest;
  use Facebook\FacebookRedirectLoginHelper;
  use Facebook\GraphUser;
  
  session_start();
  
  $bln_error      = true;
  $redirect_url   = 'http://www.example.com/';
  
  FacebookSession::setDefaultApplication( '************', '********************************' );
  
  if( isset( $_SESSION['fb_token'] ) ) {
  
      // create new session from saved access_token
      $session = new FacebookSession( $_SESSION['fb_token'] );
  
      // validate the access_token to make sure it's still valid
      try {
          if( !$session-&gt;validate() ) {
              $session = null;
          }
      } catch ( Exception $e ) {
          // catch any exceptions
          $session = null;
      }
  
  } else if( isset( $_GET['state'] ) ) {
  
      $helper = new FacebookRedirectLoginHelper( $redirect_url );
      try {
          $session = $helper-&gt;getSessionFromRedirect();
      } catch(FacebookRequestException $ex) {
          // When Facebook returns an error
          echo $ex-&gt;getMessage();
      } catch(\Exception $ex) {
          // When validation fails or other local issues
          echo $ex-&gt;getMessage();
      }
      if ( isset( $session ) &amp;&amp; $session ) {
      // Logged in.
          echo 'Logged in.';
          $bln_error = false;
      }
  
  } else {
  
      $helper         = new FacebookRedirectLoginHelper( $redirect_url, '************', '********************************' );
      $fb_login_url   = $helper-&gt;getLoginUrl();
      echo '&lt;a id="fb_link" href="' . $fb_login_url . '"&gt;Login with Facebook&lt;/a&gt;';
  
  }
  </code></pre>
  
  <p>To be even more specific:</p>
  
  <p>Initial <code>$_SESSION['FBRLH_state']</code> is same as <code>$_GET['state']</code> in <code>$fb_login_url</code> before and after redirect.
  After redirect <code>$_SESSION['FBRLH_state']</code> has a new value.</p>