1

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' ?

  • 1
    You can check the post_status for scheduled post, like you have done for published post. The post status for scheduled post is future. For more details check the codex here codex.wordpress.org/Post_Status – Maruti Mohanty Nov 4 '13 at 13:43
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');
2

I've used add_action ('transition_post_status', 'my_action'); and it works great for both posts that are published directly or in the future. The function my_action being:

function my_action ($new_status, $old_status, $post) {

    if (($old_status != 'publish') && ($new_status == 'publish')) {
        // doing things.
    }
}
2

There is an extra action hook for scheduled posts: publish_future_post - it isn't very good documented though. There are occurrences of the hook in:

The hook invokes check_and_publish_future_post(). Read the linked information, if you want to inform yourself further. Besides that the hook works like one would expect it to.

  • skid click, when checking stuff, sorry. I am using add_action('publish_post', 'tcr_tweet'); add_action('publish_future_post', 'tcr_tweet'); add_action('future_to_publish', 'tcr_tweet'); but it only seems to tweet when i publish immediately, future posts are not tweeted. I've applied these hooks to another function that sends an email to me as well and I get 3 emails (i.e. each action is triggered on a scheduled post going 'live' ) doesn't seem right to me. my logic is wrong somehow... cheers – Benny Dec 5 '13 at 13:49
  • indeed, I just need to work out what i need, it's not just scheduled posts i was hoping to hook into, normal posts too. – Benny Dec 5 '13 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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