publish_future_post in functions.php (wordpress) don't run script made with Facebook SDK 3

坚强是说给别人听的谎言 提交于 2019-12-29 08:23:55

问题


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( 'test@test.com', 'TEST1', 'TEST1' );

  error_reporting(E_ALL);
  ini_set('display_errors', '1');
include '../../../script/facebook-php-sdk-master/src/facebook.php';

wp_mail( 'test@test.com', 'TEST2', 'TEST2' );

  $facebook = new Facebook(array(
   'appId'  => '1xxxx1',
   'secret' => '2xxxx1',
   'fileUpload' => true
  ));

wp_mail( 'test@test.com', '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( 'test@test.com', 'TEST4', 'TEST4' );


  $accounts = $facebook->api('/INSERT_USER_FACEBOOK/accounts', 'GET', $params);

wp_mail( 'test@test.com', 'TEST5', 'TEST5' );


  foreach($accounts['data'] as $account) {
     if( $account['id'] == $fanpage || $account['name'] == $fanpage ){
          $fanpage_token = $account['access_token'];
     }
  }

wp_mail( 'test@test.com', 'TEST6', 'TEST6' );


 $img = "http://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( 'test@test.com', '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

    来源:https://stackoverflow.com/questions/23282194/publish-future-post-in-functions-php-wordpress-dont-run-script-made-with-face

    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!