Skip to content

Instantly share code, notes, and snippets.

@atwellpub
Created August 21, 2014 20:01
Show Gist options
  • Save atwellpub/36dcae6451cbe0013a31 to your computer and use it in GitHub Desktop.
Save atwellpub/36dcae6451cbe0013a31 to your computer and use it in GitHub Desktop.
Automatically follow a twitter user on post save
<?php
$Twitter_Auto_Follow = new Twitter_Auto_Follow();
class Twitter_Auto_Follow {
static $TwitterAPIExchange;
public function __construct() {
/* Include Twitter API Class */
include_once( WO_PATH . 'includes/twitter-api-php-master/TwitterAPIExchange.php');
self::define_static_variables();
self::load_hooks();
}
public static function define_static_variables() {
/* @devinboundnow */
self::$TwitterAPIExchange = new TwitterAPIExchange( array(
'oauth_access_token' => '2462503164-2jKjIj2lmIirUEqgrcXi3rtXKLFWqh3dIwi----',
'oauth_access_token_secret' => 'xv1sA45pzWz95EIy6cyISZ3119H1gL9Ac5C01n1L----',
'consumer_key' => '4YuaL3c1BGl89SiPRv7h5----',
'consumer_secret' => 'uLtFdSUS9jLaZHRrU8t5KW34gOyh7fFvOnx5GrQCwKnFH6----'
));
}
public static function load_hooks() {
add_action( 'save_post' , array( __CLASS__ , 'follow_user' ) , 12 );
}
public static function follow_user( $post_id ) {
global $post;
if ( !isset($post) || $post->post_type != 'job_listing' ) {
return;
}
$twitter_username = ( get_post_meta( $post->ID , '_company_twitter' , true ) ) ? get_post_meta( $post->ID , '_company_twitter' , true ) : null;
$followed = ( get_post_meta( $post->ID , '_twitter_followed' , true ) ) ? get_post_meta( $post->ID , '_twitter_followed' , true ) : null;
if ( !$twitter_username || $followed ) {
return;
}
$url = 'https://api.twitter.com/1.1/friendships/create.json';
$postfields = array('screen_name' => $twitter_username );
$requestMethod = 'POST';
$twitter_username = str_replace('@','',$twitter_username);
$response = self::$TwitterAPIExchange->setPostfields( $postfields )
->buildOauth($url, $requestMethod)
->performRequest();
update_post_meta( $post->ID , '_twitter_followed' , true );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment