File Included In Wordpress Header Not Available In Body

I included the Facebook SDK in Wordpress header.php, and within this file, the SDK works fine. However, when I try to use the SDK in a page that uses the header, I get errors saying certain objects weren't defined etc. As if the file was never included.

Basically, I can get the user's FB info to display on the header of the page, but nowhere else.

EDIT: Here's part of the header.php file:

<?php
include_once('facebook-php-sdk/src/facebook.php');
$config = array();
$config['appId'] = '***********';
$config['secret'] = '***********************';
$config['cookie'] = true;
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$user_profile = $facebook->api('/me','GET');

echo $fbUsername = $user_profile['name'];//WORKS HERE
?>

But on a page:

<?php
get_header();
echo $fbUsername;//not working;
?>

Solutions

I think you are including this header in every file you have. So, a new facebook object created each time- so may be the new facebook session- that's the wrong way to do it!

The error:

"An active access token must be used to query information about the current user

is due to the fact that there is not current user in the session so you cannot call any api like: /me...

So dont create FB object again and again, instead create it only once - at the start of the application. Use that same $facebook and then main thing- the session/active access token can expire at any time, so you should do a check:

$user_id = $facebook->getUser();
if($user_id)
  $user_profile = $facebook->api('/me','GET'); 
else
   // get the login url and create new session 

(you can follow sdk sample for the same)


You can also save the name or other fb info you are using in your session variable once just like-

session_start();
$_SESSION['fb_user_name'] = $user_profile['name'];

instead of fetching them again and again.

Tags: PHP / Facebook / Wordpress

Similar questions

Header menu included in exported excel sheet in wordpress front pages
I have to export dynamic html table data in wordpress front users data comes fine but header menu also get included in excel sheet Same code working fine in wordpress backend. Here is screenshot https://prnt.sc/qyw9v6 Please suggest to correct this.
WordPress update_option - use in a file where the WP files are not included
I'm writing a WP plugin and have an issue. In the pludin's settings area, there is a form, where the content is sent to a file (save.php) trough AJAX. So, the save.php file has no access to WP's update_option function. How can I use the option functions in the save.php file? Thx!
add styles if custom header was included
I have template which includes(get_header("modern");) header-modern.php, i need to add styles(in functions.php) if included this header, how i can do this ? if(is_page_template('header-modern.php')){ wp_enqueue_style('landing-style', get_template_directory_uri() . '/css/navbar.css'); } i need correct condition.
Shortcode not passing variable to included file
I set up a shortcode which includes a standard php/html form file. In the form I have a hidden field where the value is a variable which is meant to be available in the form via the shortcode attributes. The problem is that the value for clientId is not being passed from: Instead I am getting the value defined as the default (NULL). See the code be...
locate_template function - File not getting included
I am using the above path and method to include a file in functions.php, but that is not happening. The file doesn't seem to include. Am I doing a mistake?
the file placed in the child theme is not included
I have modified the theme.php file located in the parent theme in the "Includes" folder, and I have placed it in the child theme also in the "Includes" folder, but my changes are not included. WordPress only reads the file placed in the parent theme folder. What is the reason for this? What should I do the modifications are included when the file i...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.