0

I have wordpress 4.1 with the following plugin installed:

  • iThemes Security
  • WordPress SEO

I plugged in a function the facebook sdk.

This is the code:

//Footer
add_action('wp_footer', 'my_footer');
function my_footer() {
    $post_to_check = get_post(get_the_ID());
    if ( is_singular() && has_shortcode( $post_to_check->post_content, 'myshortcode' ) ) {
?>
<script>
var DEBUG = true;

jQuery(document).ready(function($){
    $.ajax({
        type : "POST",
        url : "index.php",
        data : { action : "value" },
        success : function(response){
            // the server has finished executing PHP and has returned something, so display it!
            if(DEBUG){ console.log('AJAX done...'); }
        }
    });
});
</script>
<?php
    }
}

//ajax
add_action('init', 'my_request');
function my_request() {
    if ( isset($_POST['action']) && $_POST['action'] == 'value' ) {

        session_start();

        require_once __DIR__ . "/sdk/autoload.php";

        use Facebook\FacebookSession;
        use Facebook\FacebookRequest;
        use Facebook\GraphUser;
        use Facebook\FacebookRedirectLoginHelper;
        use Facebook\FacebookSDKException;

        $app_id     = getAppId();
        $app_secret = getAppSecret();
        $rdr_fb_url = curPageURL();
        $required_scope = 'public_profile, email';

        if(empty($app_id) || empty($app_secret))
            exit('Error.');

        FacebookSession::setDefaultApplication($app_id , $app_secret);

        $helper = new FacebookRedirectLoginHelper($rdr_fb_url);

        try {
          $session = $helper->getSessionFromRedirect();
        } catch(FacebookRequestException $ex) {
            // When Facebook returns an error
            die(" Error : " . $ex->getMessage());
        } catch(\Exception $ex) {
            // When validation fails or other local issues
            die(" Error : " . $ex->getMessage());
        }

        var_dump($session);

        if ($session){
            $user_profile = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
            echo 'Hi'.$user_profile->getName();

        }else{
            //display login url
            $login_url = $helper->getLoginUrl( array( 'scope' => $required_scope ) );
        }

?>
<div class="login">
    <a href="<?php echo $login_url; ?>">Login</a>
</div>
<?php
    exit();
  }
}

Works fine if i use this outside of wordpress. In wp var_dump($session); retrive always null. (no error given) Even after accepting the terms of the application. it is as if he could not get the code in the url.

I also tried to disable the plugin, but nothing.

I need php, please don't tell me to use javascript.

Sorry for my bad english :P

Edit: I use the permalink: http://www.myste.com/%year%/%monthnum%/%postname%/

I think the problem is the rewrite rule that hide a facebook get.

1 Answer 1

-1

Solved: I entered the code outside of the ajax call, in function my_footer() {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.