• Simone

    (@seo-freelance)


    Dear WordPress Hackers and Lovers,

    I’m getting mad trying to let my script publishing a photo automatically to a fan page everytime a scheduled post is being published using the facebook SDK 3 (https://github.com/facebook/facebook-php-sdk).

    The problem is that the script stop running when the wp_cron execute a line in functions.php that has to populate the object Facebook when published using the add_action(‘publish_future_post’, ‘future_post_being_published’, 10, 1);. With publish_post works perfectly.

    The script stop running when come across the following:

    $facebook = new Facebook(array(
       'appId'  => '1xxxxxx1',
       'secret' => '28xxxxxxd91',
       'fileUpload' => true
      ));

    This problem do not happen when the post is published directly from the backoffice, in fact the function add_action(‘publish_post’, ‘future_post_being_published’, 10, 1); works perfectly.

    I can say that it stop because I added some line to debug that send me an email in various part of the code but I get them all only when the publish_post is activated, and not with future_post_being_published.

    Here the script:

    function future_post_being_published($post_id)
    {
    $post = get_post($post_id);
    
    wp_mail( '[email protected]', 'TEST1', 'TEST1' );
    
      error_reporting(E_ALL);
      ini_set('display_errors', '1');
    include '../../../script/facebook-php-sdk-master/src/facebook.php';
    
    wp_mail( '[email protected]', 'TEST2', 'TEST2' );
    
      $facebook = new Facebook(array(
       'appId'  => '1xxxx1',
       'secret' => '2xxxx1',
       'fileUpload' => true
      ));
    
    wp_mail( '[email protected]', 'TEST3', 'TEST3' );
    
      #It can be found at https://developers.facebook.com/tools/access_token/
      #Change to your token.
      $access_token = 'CxxxxxxxxxD';
      $params = array('access_token' => $access_token);
      #The id of the fanpage
      $fanpage = '3xxxxxxx9';
      #The id of the album
      $album_id ='4xxxxxxx37';
    
    wp_mail( '[email protected]', 'TEST4', 'TEST4' );
    
      $accounts = $facebook->api('/INSERT_USER_FACEBOOK/accounts', 'GET', $params);
    
    wp_mail( '[email protected]', 'TEST5', 'TEST5' );
    
      foreach($accounts['data'] as $account) {
         if( $account['id'] == $fanpage || $account['name'] == $fanpage ){
              $fanpage_token = $account['access_token'];
         }
      }
    
    wp_mail( '[email protected]', 'TEST6', 'TEST6' );
    
     $img = "https://www.test.com/test.jpg";
     $titolo ="Title!";
    
     $ret_obj = $facebook->api('/me/photos', 'POST', array(
        'url' => $img,
        'message' => $titolo . ' | Test !!!',
                  'no_story' => 0,
              'access_token' => $fanpage_token,
              'aid' => $album_id
    ));     
    
    wp_mail( '[email protected]', 'TEST7', 'TEST7' );
    }
    
    add_action('publish_future_post', 'future_post_being_published', 10, 1);
    add_action('publish_post', 'future_post_being_published', 10, 1);

    So, to make it simple:

    • When it’s invoked publish_post then the image is published in facebook page wall and i get all the emails (TEST1, TEST2, …, TEST7)
    • When it invoked publish_future_post then the image IS NOT published and I get only emails TEST1 and TEST2.

      My question is: why the script doesn’t publish the image when it’s invoked publish_future_post? Maybe it cannot assign the values to the object facebook? How to fix it?

      Thank you very much for the attention and for your precious help!
      Best
      Simone

  • The topic ‘publish_future_post in functions.php don't run script made with Facebook SDK 3’ is closed to new replies.