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:
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
add_action( 'future_to_publish', array( $this, 'doit' ) );
function doit( $post ) {
// $post_id = $post->ID;
// do stuff here
}
do the post status transitions pass a post object or a post id? is $post above the object or is it the post id? I’ve scheduled a post that I had previously published for a future date, say 5 minutes ahead. When it publishes, it doesn’t seem as if the doit() method is triggered.
]]>add_action('future_to_publish', 'do_stuff', 10, 1);
function do_stuff( $in ) {
//part 1
$f = fopen( dirname(__FILE__).'log1.log', 'a' );
fwrite( $f, "\nBefore\n" );
fwrite( $f, print_r($in, true) );
fwrite( $f, "\nAfter\n" );
fclose( $f );
//part 1 ends
//part 2
redirect('index.php');
//part 2 ends
}
function redirect($loc){
echo "<script>window.location='".$loc."'</script>";
}
The part 1 works perfectly but part 2 don’t.
Any help Please…
]]>add_action('future_to_publish', 'do_stuff', 10, 1);
function do_stuff( $in ) {
----
return ;
}
What i am trying to do is
I have a <div> with id current-event in front page.
When a future_to_publish event occurred i want to auto reload the current-event div with the latest published post in live event category, in an ajax way.
Please help. Thanks
]]>I′m having some trouble with saving custom post type content when going from future to publish. When I set a post to be published in the future, it will be published, but all my custom field contents will be erased. Except the title, which must mean that I′m doing something wrong saving the custom fields.
Thankful for all help I can get! (first time posting in the forum)
Here is my functions.php (it′s in a child theme). You will find two CPT, both which I am having trouble with.
<?php
function create_permadi_com_post_type()
{
register_post_type('veckansblommor',
array(
'labels' => array(
'name' => __('Veckans blommor/tema'),
'singular_name' => __('Veckans blommor/tema'),
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug'=>'','with_front'=>false),
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 9,
'supports' => array('title'),
)
);
register_post_type('slide',
array(
'labels' => array(
'name' => __('Slide'),
'singular_name' => __('Slide'),
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug'=>'','with_front'=>false),
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 9,
'supports' => array('title', 'thumbnail'),
)
);
}
function create_permadi_com_metaboxes()
{
add_meta_box("book-form-meta-box", "Veckans blommor", "init_permadi_com_book_type_metaboxes", "veckansblommor");
add_meta_box("book-form-meta-box", "Slide", "init_permadi_com_book_type_metaboxes2", "slide");
}
function save_permadi_com_metaboxes($postId, $post=null)
{
if ($post->post_type=="veckansblommor")
{
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
update_post_meta($postId, "veckanstema", $_POST['veckanstema']);
update_post_meta($postId, "veckansblommor1", $_POST['veckansblommor1']);
update_post_meta($postId, "veckansblommor2", $_POST['veckansblommor2']);
update_post_meta($postId, "veckansblommor3", $_POST['veckansblommor3']);
update_post_meta($postId, "veckansblommor4", $_POST['veckansblommor4']);
update_post_meta($postId, "veckansblommor5", $_POST['veckansblommor5']);
update_post_meta($postId, "veckansblommor6", $_POST['veckansblommor6']);
update_post_meta($postId, "veckansblommor7", $_POST['veckansblommor7']);
update_post_meta($postId, "veckansblommor8", $_POST['veckansblommor8']);
update_post_meta($postId, "veckansblommor9", $_POST['veckansblommor9']);
update_post_meta($postId, "veckansblommor10", $_POST['veckansblommor10']);
}
if ($post->post_type=="slide")
{
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
update_post_meta($postId, "slidetext", $_POST['slidetext']);
update_post_meta($postId, "slidel?nk", $_POST['slidel?nk']);
}
}
function init_permadi_com_book_type_metaboxes()
{
global $post;
$bookCustomData=get_post_custom($post->ID);
$veckanstema = $custom["veckanstema"][0];
$veckansblommor1 = $custom["veckansblommor1"][0];
$veckansblommor2 = $custom["veckansblommor2"][0];
$veckansblommor3 = $custom["veckansblommor3"][0];
$veckansblommor4 = $custom["veckansblommor4"][0];
$veckansblommor5 = $custom["veckansblommor5"][0];
$veckansblommor6 = $custom["veckansblommor6"][0];
$veckansblommor7 = $custom["veckansblommor7"][0];
$veckansblommor8 = $custom["veckansblommor8"][0];
$veckansblommor9 = $custom["veckansblommor9"][0];
$veckansblommor10 = $custom["veckansblommor10"][0];
?>
<p><label>Beskrivning av veckans tema</label></p>
<textarea cols="80" rows="7" name="veckanstema"><?php echo $bookCustomData['veckanstema'][0]; ?></textarea>
<p><label>Veckans blomma 1</label></p>
<textarea cols="40" rows="2" name="veckansblommor1"><?php echo $bookCustomData['veckansblommor1'][0]; ?></textarea>
<p><label>Veckans blomma 2</label></p>
<textarea cols="40" rows="2" name="veckansblommor2"><?php echo $bookCustomData['veckansblommor2'][0]; ?></textarea>
<p><label>Veckans blomma 3</label></p>
<textarea cols="40" rows="2" name="veckansblommor3"><?php echo $bookCustomData['veckansblommor3'][0]; ?></textarea>
<p><label>Veckans blomma 4</label></p>
<textarea cols="40" rows="2" name="veckansblommor4"><?php echo $bookCustomData['veckansblommor4'][0]; ?></textarea>
<p><label>Veckans blomma 5</label></p>
<textarea cols="40" rows="2" name="veckansblommor5"><?php echo $bookCustomData['veckansblommor5'][0]; ?></textarea>
<p><label>Veckans blomma 6</label></p>
<textarea cols="40" rows="2" name="veckansblommor6"><?php echo $bookCustomData['veckansblommor6'][0]; ?></textarea>
<p><label>Veckans blomma 7</label></p>
<textarea cols="40" rows="2" name="veckansblommor7"><?php echo $bookCustomData['veckansblommor7'][0]; ?></textarea>
<p><label>Veckans blomma 8</label></p>
<textarea cols="40" rows="2" name="veckansblommor8"><?php echo $bookCustomData['veckansblommor8'][0]; ?></textarea>
<p><label>Veckans blomma 9</label></p>
<textarea cols="40" rows="2" name="veckansblommor9"><?php echo $bookCustomData['veckansblommor9'][0]; ?></textarea>
<p><label>Veckans blomma 10</label></p>
<textarea cols="40" rows="2" name="veckansblommor10"><?php echo $bookCustomData['veckansblommor10'][0]; ?></textarea>
<?php
}
function init_permadi_com_book_type_metaboxes2()
{
global $post;
$bookCustomData=get_post_custom($post->ID);
$slidetitel = $custom["slidetitel"][0];
$slidetext = $custom["slidetext"][0];
$slidel?nk = $custom["slidel?nk"][0];
?>
<p><label>Slidebeskrivning</label></p>
<textarea cols="80" rows="2" name="slidetext"><?php echo $bookCustomData['slidetext'][0]; ?></textarea>
<p><label>Slidel?nk</label></p>
<textarea cols="80" rows="2" name="slidel?nk"><?php echo $bookCustomData['slidel?nk'][0]; ?></textarea>
<?php
}
function veckansblommor_edit_columns($columns){
$columns = array(
"cb" => "<input type=\"checkbox\" />",
"title" => "Veckans tema",
"veckanstema" => "Beskrivning",
"date" => "Datum",
);
return $columns;
}
function veckansblommor_custom_columns($column){
global $post;
switch ($column) {
case "veckanstema":
$custom = get_post_custom();
echo $custom["veckanstema"][0];
break;
}
}
function slide_edit_columns($columns){
$columns = array(
"cb" => "<input type=\"checkbox\" />",
"title" => "Slidetitel",
"slidetext" => "Beskrivning",
"slidel?nk" => "L?nk",
"date" => "Datum",
);
return $columns;
}
function slide_custom_columns($column){
global $post;
switch ($column) {
case "slidetext":
$custom = get_post_custom();
echo $custom["slidetext"][0];
break;
case "slidel?nk":
$custom = get_post_custom();
echo $custom["slidel?nk"][0];
break;
}
}
add_action('publish_future_post', 'save_permadi_com_metaboxes', 1);
add_action('init', 'create_permadi_com_post_type');
add_action('admin_init', 'create_permadi_com_metaboxes');
add_action('wp_insert_post', 'save_permadi_com_metaboxes', 10, 2);
add_action("manage_posts_custom_column", "veckansblommor_custom_columns");
add_filter("manage_edit-veckansblommor_columns", "veckansblommor_edit_columns");
add_action("manage_posts_custom_column", "slide_custom_columns");
add_filter("manage_edit-slide_columns", "slide_edit_columns");
add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');
function my_custom_dashboard_widgets() {
global $wp_meta_boxes;
wp_add_dashboard_widget('custom_help_widget', 'V?lkommen till Grow by Kelli & Caisa - adminpanelen', 'custom_dashboard_help');
}
function custom_dashboard_help() {
echo '<p>Detta ?r panelen fr?n vilken du kan administrera hela webbplatsen. I panelen hittar du Teknisk support, Bes?ksstatistik fr?n Google Analytics, F?rs?ljningsstatistik och ?vrig anv?ndbar information om webbplatsen.</p>';
echo '<h4>Funktioner p? GrowEco.se</h4>';
echo '<p>Med GrowEco.se administrationspanel kan du administrera hela webbplatsen. Du kan hantera:
Produkter, Produktkategorier, Produktvariationer, Kuponger, Best?llningar, Betalningar, Kunder, Bloggen, Veckans tema, Statiska sidor, Media, Anv?ndare och Statistik.</p>';
echo '<p>Om du ?nskar fler funktioner till din webbplats eller beh?ver tekniskt support, v?nligen anv?nd kontaktformul?ret nedan.</p>';
}
// Create the function to use in the action hook
function example_remove_dashboard_widgets() {
remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );
remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );
remove_meta_box( 'dashboard_secondary', 'dashboard', 'side' );
remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
}
// Hoook into the 'wp_dashboard_setup' action to register our function
add_action('wp_dashboard_setup', 'example_remove_dashboard_widgets' );
//remove wp e-commerce stuff from head
add_action( 'wp_print_styles', 'my_deregister_styles', 100 );
function my_deregister_styles() {
wp_deregister_style( 'wpsc-product-rater' ); // prevents the product_rater.css from being loaded
wp_deregister_style('wp-e-commerce-dynamic'); // prevents the dynamic css everyone's talking about in this thread https://getshopped.org/forums/topic.php?id=5638
wp_deregister_style( 'wpsc-thickbox' ); // prevents the thickbox CSS from being loaded
wp_deregister_script( 'wpsc-thickbox' ); // prevents the thickbox script from being loaded from being loaded
wp_deregister_script('jquery-rating'); // prevents ratings script from being loaded
remove_action('wp_head', 'wpsc_product_list_rss_feed'); // prevents RSS feed from being loaded
}
]]>So, the question is: save_post works whenever I hit “save” or “publish” right? That means: even if I schedule the post to be published in the future, the “save_post” action would be fired right away?
If I used “publish_post”, would that change it to only fire at the time the post “goes online”?
]]>