publish_post hook isn’t working for scheduled posts

I had a bit of an issue with wp-to-twitter publishing a tweet linking to a post that was set as ‘pending’ that i had emailed to my wordpress.org site. The email included a footer, which included my mobile number.

I decided to create my own plugin.

add_action('publish_post', 'tcr_tweet');

/* the function */
function tcr_tweet($postID)
{

    if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {
            /* get the post that's being published */
            $post = get_post($postID); 
            $post_title = $post->post_title;

            /* get the author of the post */
            $author_id=$post->post_author;
            /* author needs a twitterid in their meta data*/
            $author = get_the_author_meta('twitterid',$author_id );

            /* get the permalink and shorten it */
            $url = get_permalink($postID);
            $short_url = getBitlyUrl($url);

            //check to make sure the tweet is within the 140 char limit
            //if not, shorten and place ellipsis and leave room for link. 
                    if (strlen($post_title) + strlen($short_url) > 100) {
                       $total_len = strlen($post_title) + strlen($short_url);
                       $over_flow_count = $total_len - 100;
                       $post_title = substr($post_title,0,strlen($post_title) - $over_flow_count - 3);
                       $post_title .= '...';                
                    }

            //add in the shortened bit.ly link
            $message =  "New: ".$post_title." - ".$short_url." by @".$author." #hashtag";

             if ( $post->post_status != 'publish' ) return;
            //call the tweet function to tweet out the message
            goTweet($message);
    }

}

my plugin is a simple plugin, it calls out to my bit.ly function and then to a different function that tweets, these function are used elsewhere and work perfectly well.

My issue is that if i schedule a post nothing gets tweeted, if i click publish on a new post i get a white screen, but tweet is sent.

How can i target scheduled posts correctly ? I’ve looked at my $_POST data and it seems ok. Code seems pretty straightforward, so i’m missing something.. thanks

Edit:

I have some confusion on how WordPress works with scheduled posts post_status="future" when the time comes and the post needs to be made live, surely it becomes post_status="publish" as it’s no longer a ‘future’ post. so my function should be triggered when

 add_action('publish_post', 'tcr_tweet'); 
 add_action('publish_future_post', 'tcr_tweet'); 
 add_action('future_to_publish', 'tcr_tweet');

these actions are triggered. Do I need to check that the date has passed instead, if a post_status stays as ‘future’ ?

3 Answers
3

I reworked (added another version of) my function to remove the if statement to check post status, as the post is scheduled to publish it turns out i don’t need to check it again.

/* the function */
function tcr_tweet2($postID)
{

            /* get the post that's being published */
            $post = get_post($postID); 
            $post_title = $post->post_title;

            /* get the author of the post */
            $author_id=$post->post_author;
            /* author needs a twitterid in their meta data*/
            $author = get_the_author_meta('twitterid',$author_id );

            /* get the permalink and shorten it */
            $url = get_permalink($postID);
            $short_url = getBitlyUrl($url);

            //check to make sure the tweet is within the 140 char limit
            //if not, shorten and place ellipsis and leave room for link. 
                    if (strlen($post_title) + strlen($short_url) > 100) {
                       $total_len = strlen($post_title) + strlen($short_url);
                       $over_flow_count = $total_len - 100;
                       $post_title = substr($post_title,0,strlen($post_title) - $over_flow_count - 3);
                       $post_title .= '...';                
                    }

            //add in the shortened bit.ly link
            $message =  "New: ".$post_title." - ".$short_url." by @".$author." #hashtag";

             if ( $post->post_status != 'publish' ) return;
            //call the tweet function to tweet out the message
            goTweet($message);
}

I can then use the following hook just for this version and it works.

 add_action('future_to_publish', 'tcr_tweet2');

Leave a Comment