WordPress.org

Ready to get started?Download WordPress

Forums

Facebook SDK not working in wordpres template (7 posts)

  1. tWH
    Member
    Posted 3 years ago #

    Hi!

    I'm trying to implement the Facebook php sdk in a WordPress template, but I just can't get it to work. The supplied example.php works without problems, but the same code in a template doesn't. I boiled the problem down to the getSession() method, which simply always returns null.

    Since example.php works i figure the problem is somehow related to WordPress. I really can't think of any reason why it shouldn't work in WordPress.

    Any ideas?

  2. tWH
    Member
    Posted 3 years ago #

    I've found a temporary solution. In getSession() the following rows remove backslashes only if get_magic_quotes_gpc() is true:

    parse_str(trim(
       get_magic_quotes_gpc()
            ? stripslashes($_COOKIE[$cookieName])
            : $_COOKIE[$cookieName],
        '"'
    ), $session);

    However, for some reason backslashes in the cookie are added despite get_magic_quotes_gpc() being false. Thus changing the above rows to:

    parse_str(trim(
        stripslashes($_COOKIE[$cookieName]),
        '"'
    ), $session);

    solves the problem. I'm not sure though whether not not this creates any other problems.

  3. scchu
    Member
    Posted 3 years ago #

    Hi tWH,

    Thank you for the finding. That solved my two days of agony.

    On my PHP install, "magic_quotes_gpc" is off by default. And it seems like it will be deprecated in future versions of PHP.

    I implemented a similar solution except that I check for it:

    parse_str(trim(
        function_exists( "get_magic_quotes_gpc" )
          ? stripslashes($_COOKIE[$cookieName])
          : $_COOKIE[$cookieName],
        '"'
      ), $session);

    That way the code is more portable for different environments where we simply can't know the status of "get_magic_quotes_gpc".

    Thanks again!

  4. scchu
    Member
    Posted 3 years ago #

    Actually what I posted is not idea either.

    It turns out this is an issue with WordPress' internal wp_magic_quotes() function screwing with facebook php-sdk.

    So what I did instead was to turn force turn on magic_quotes_gpc for wordpress only.

    To do that, at the root of WordPress director, create a .htaccess file (if there isn't already one). And paste the follow code to force it on.

    php_flag magic_quotes_gpc on

    Cheers...

  5. David C
    Member
    Posted 3 years ago #

    tWH, thanks so much for the fix! I've spent the last two days trying to figure out why the cookie was correctly being set but the session was always null.

    I guess we'll have to go with your hack until Facebook fixes this so it works correctly with WordPress.

    ~David

  6. lruellan
    Member
    Posted 3 years ago #

    I'm having the exact same issue! Thanks for coming up with a solution.

    Lothaire

  7. ellebillias
    Member
    Posted 2 years ago #

    I'm having similar problems! But in my php.ini file magic quotes is set to on by default

    magic_quotes_gpc on

    what would I use to make the Facebook getSession() work on my WordPress?

Topic Closed

This topic has been closed to new replies.

About this Topic