Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Should be simple - I'd like WP to tweet the content of a post once (when it's published the first time) if the post format is 'status'.

The code below isn't returning an error on publish, nor is it tweeting OR updating the post metadata, which leads me to believe I'm either using the hook incorrectly or there's something wrong with checking the post type as I have.

This is currently living in my functions.php

I'm using Matt Harris' tmhOAuth and have successfully tweeted from a standalone tweet.php using most of the function below.

Keys and secrets removed for security.

function posse_twitter( $post ) {
    // check post type if necessary
    if ( $post->post_type != 'status' ) return;

    $post_id = $post->ID;
    $tweet_content = $post->post_content;

    if ( !get_post_meta( $post_id, 'tweeted', $single = true ) ) {
        // ...run code once
        //Include Libraries
        require 'libraries/tmhOAuth/tmhOAuth.php';
        require 'libraries/tmhOAuth/tmhUtilities.php';
        $tmhOAuth = new tmhOAuth(array(
          'consumer_key'    => 'XXXXX',
          'consumer_secret' => 'XXXXX',
          'user_token'      => 'XXXXX',
          'user_secret'     => 'XXXXX',
        ));

        $code = $tmhOAuth->request('POST', $tmhOAuth->url('1/statuses/update'), array(
          'status' => $tweet_content
        ));

        if ($code == 200) {
          tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
        } else {
          tmhUtilities::pr($tmhOAuth->response['response']);
        }
        update_post_meta( $post_id, 'tweeted', true );
    }
}

add_action( 'draft_to_published', 'posse_twitter' );
share|improve this question
    
Have you already checked the contents of the vars? How long do you get something? Is $tweet_content/$post->post_content set? If so: Delete the meta value (or update to false as long as you're developing, so you can test it). –  kaiser Oct 29 '12 at 23:14
    
@kaiser what's the best way to check for contents where I'm using them? i.e. since the function runs on publish? (I would just echo, but I'm not sure where/when that will get spit out.) –  Edbury Oct 29 '12 at 23:40
1  
var_dump( $post->post_content ); inside... or have I understood you wrong? Seems like a funny question for someone writing such advanced code, so I guess I'm missing the point of your question. –  kaiser Oct 30 '12 at 0:09
    
@kaiser dumping the contents of the post won't do much in this context, the results of the action aren't visible to the user. –  doublesharp Oct 30 '12 at 0:16
    
@kaiser I did var_dump into the error log once the function was executing. Dumping the $post object gives me int(730), which is the correct post ID. Dumping anything from the object (e.g. $post->post_type) returns NULL. –  Edbury Oct 30 '12 at 15:05
show 1 more comment

2 Answers

My guess is that you aren't hitting the {$old_status}_to_{$new_status} action, which is what you are hooking to with draft_to_published - for new posts there is no guarantee that the "old" status is actually "draft". Have you tried using the {$new_status}_{$post->post_type} action, which in your case would be publish_status?

share|improve this answer
    
Can I get an example of what variable hook implementation looks like? It's not add_action( {$new_status}_{$post->post_type}, 'posse_twitter' ); is it? –  Edbury Oct 29 '12 at 23:40
1  
no, it would be add_action('publish_status', 'posse_twitter' ); - "publish" is your new status, and "status" is your post type. –  doublesharp Oct 29 '12 at 23:44
1  
For debugging purposes you could add an error_log('executing posse_twitter()') at the very top of your function and then check your php error log to make sure it is getting called. –  doublesharp Oct 29 '12 at 23:45
    
publish_status wasn't working; tried a standard publish_post. Finally got the function to execute (logged), but nothin else so far. –  Edbury Oct 30 '12 at 14:54
    
@EdburyEnegren what version of WP are you on? Can you update with your latest code? –  doublesharp Oct 30 '12 at 14:56
show 1 more comment
up vote 1 down vote accepted

$post isn't available during {$new_status}_{$post->post_type}, but $post_ID is. We pass that to the function and then call in $post globally.

I should have also been testing using get_post_format(), not for a type.

After those corrections, everything seems to be running smoothly with the caveat that if the post is not auto-saved or saved as a draft prior to hitting publish, things like $post->post_content will still be empty during {$new_status}_{$post->post_type}.

New code below.

function posse_twitter( $post_ID ) {
    global $post;
    // check post type if necessary
    if ( get_post_format( $post->ID ) != 'status' ) return;

    $post_id = $post->ID;
    $tweet_content = $post->post_content;

    if ( !get_post_meta( $post_id, 'tweeted', $single = true ) ) {
        // ...run code once
        require 'libraries/tmhOAuth/tmhOAuth.php';
        require 'libraries/tmhOAuth/tmhUtilities.php';
        $tmhOAuth = new tmhOAuth(array(
          'consumer_key'    => 'XXXX',
          'consumer_secret' => 'XXXX',
          'user_token'      => 'XXXX',
          'user_secret'     => 'XXXX',
        ));

        $code = $tmhOAuth->request('POST', $tmhOAuth->url('1/statuses/update'), array(
          'status' => $tweet_content
        ));

        update_post_meta( $post_id, 'tweeted', true );
    }
}

add_action( 'publish_post', 'posse_twitter' );

Handy: Adam Brown's API reference

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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