Created
January 25, 2017 19:35
-
-
Save cdevroe/972f35b1edd13962a780d1b301c056c0 to your computer and use it in GitHub Desktop.
Publish posts, statuses, images, to Twitter from WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Tweet Statuses | |
* Description: A simple plugin that will tweet a status when published. | |
* Author: Colin Devroe | |
* Author URI: http://cdevroe.com/ | |
* Version: 0.1.0 | |
* | |
* TwitterAPIExchange is here: http://github.com/j7mbo/twitter-api-php | |
*/ | |
function cdevroe_tweet_status( $post_id, $post ) { | |
require_once('TwitterAPIExchange.php'); | |
$format = get_post_format( $post ); | |
$twitter = new TwitterAPIExchange(array( | |
'oauth_access_token' => "", | |
'oauth_access_token_secret' => "", | |
'consumer_key' => "", | |
'consumer_secret' => "" | |
)); | |
if ( $post->post_title != '' ) : // Likely not a status update | |
if ( $format == 'image' ) : | |
$status = 'Image: ' . $post->post_title; | |
else: | |
$status = $post->post_title; // Strip all HTML | |
endif; | |
if ( strlen($status) > 110 ) : // Limit to 140 characters regardless of content length | |
$status = substr( $status, 0, 110 ) . '... ' . get_permalink( $post ); | |
else: | |
$status .= ' ' . get_permalink( $post ); | |
endif; | |
else : // Likely a status update | |
$status = wp_strip_all_tags( $post->post_content ); // Strip all HTML | |
if ( strlen($status) > 140 ) : // Limit to 140 characters regardless of content length | |
$status = substr( $status, 0, 110 ) . '... ' . get_permalink( $post ); | |
endif; | |
endif; | |
$twitter_response = $twitter->buildOauth('https://api.twitter.com/1.1/statuses/update.json', 'POST') | |
->setPostfields(array( | |
'status' => $status | |
)) | |
->performRequest(); | |
return; | |
} | |
add_action( 'publish_post', 'cdevroe_tweet_status', 10, 2 ); // post_id, post obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment