1

WordPress apparently stores post format information outside of the $post object. During publish_post this isn't (usually) a problem because you can check on formats via get_post_format( $post->ID ) outside the loop.

This doesn't appear to work during xmlrpc_publish_post or app_publish_post. People have to be working around this to get plugin/theme functions working on mobile/desktop apps.

My specific code is below in case it's an error on my end.

function posse_twitter( $post_ID ) {
    error_log('Executing posse_twitter()');
    global $post;
    // check post format if necessary
    if ( get_post_format( $post->ID ) != 'status' ) return;
    error_log('posse_twitter() made it past format check');

    $post_id = $post->ID;
    $shortlink = wp_get_shortlink();
    $tweet_content = $post->post_content.' '.$shortlink;

    // ...run code once
    if ( !get_post_meta( $post_id, 'tweeted', $single = true ) ) {

        error_log('posse_twitter() not tweeted before');

        // require the relevant libraries
        require get_template_directory() .'/inc/posse/libraries/tmhOAuth/tmhOAuth.php';
        require get_template_directory() .'/inc/posse/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
        ));

        error_log('posse_twitter() made it past tmhOAuth, should be on Twitter now');

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

add_action( 'xmlrpc_publish_post', 'posse_twitter' ); 
add_action( 'app_publish_post', 'posse_twitter' );
add_action( 'publish_post', 'posse_twitter' );

error_log()s included since I've been debugging this. It's firing when xmlrpc_publish_post and app_publish_post get hit, but it doesn't make it past the format check.

Update: This also doesn't work even if I save the post as a draft first (via XML-RPC).

2

This is because of a variable mismatch. Your function accepts $post_ID, but you don't actually use it. You're instead trying to reference a global $post object and doing your post format check with $post->ID. With the XML-RPC request, this won't work.

Rewrite your function to use get_post() to fetch a post object from the passed-in ID:

function posse_twitter( $post_ID ) {
    error_log('Executing posse_twitter()');
    $post = get_post( $post_ID );
    // check post format if necessary
    if ( get_post_format( $post->ID ) != 'status' ) return;
    error_log('posse_twitter() made it past format check');

    ...
}
  • Ugh, totally jammed myself up on that one. Worked perfectly, thanks! – Edbury Nov 2 '12 at 20:10

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.