Adding a Twitter feed

Playing with adding a twitter feed directly into my theme.

<?php
function twitter_import() {
  require "twitter.lib.php";
  $username = "mdrude";
  $password = "<password>";
  $twitter = new Twitter($username, $password);
  $old_id = get_option('milly_twitter_old_id');
  $xml = $twitter->getUserTimeline("max_id=$old_id");
  $twitter_status = new SimpleXMLElement($xml);
  global $user_ID;

  foreach($twitter_status->status as $status){
    if ( $status->id > $old_id ) {
      echo "running";
      $tweet_text = $status->text;
      $created_at = $status->created_at;
      $tweet_date = date("Y-m-d H:i:s", strtotime($created_at));
      $tweet_id = $status->id;
      $new_post = array(
        'post_title' => $tweet_text,
        'post_content' => $tweet_text,
        'post_status' => 'publish',
        'post_date_gmt' => $tweet_date,
        'post_author' => $user_ID,
        'post_type' => 'twitter'
      );
      $post_id = wp_insert_post($new_post);
      add_post_meta($post_id, 'aktt_twitter_id', "$tweet_id");
      add_post_meta($post_id, 'twitter_id', "$tweet_id");
      update_option('milly_twitter_old_id', "$tweet_id");
    }
  }
}
add_filter('cron_schedules', 'cron_add_milly');
function cron_add_milly( $schedules )
{
        $schedules['milly'] = array(
                'interval' => 300,
                'display' => __('Every 5 Mins')
        );        return $schedules;}
if (!wp_next_scheduled('twitter_import_hook')) {
  echo "Updating schedule";
  wp_schedule_event( time(), 'milly', 'twitter_import_hook' );
}
add_action( 'twitter_import_hook', 'twitter_import' );
?>

You may also download this snip from it’s gist.

Advertisements