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.

The below code is used to pull in my Tweets and post them as individual blog posts on my Wordpress site.

I would like to add a "Follow me on Twitter link" after the tweet when it is displayed in the main body of the post. How would I do this?

So for example, it want it to look like:

Title - Truncated Tweet

Body - Tweet in full

Post tweet text - Follow me on Twitter (with a link)

Here is the code. Any help would be fantastic.

<?php

function Keyring_Twitter_Importer() {

class Keyring_Twitter_Importer extends Keyring_Importer_Base {
    const SLUG              = 'twitter';    // e.g. 'twitter' (should match a service in Keyring)
    const LABEL             = 'Twitter';    // e.g. 'Twitter'
    const KEYRING_SERVICE   = 'Keyring_Service_Twitter';    // Full class name of the Keyring_Service this importer requires
    const REQUESTS_PER_LOAD = 3;     // How many remote requests should be made before reloading the page?

    var $auto_import = false;

    function __construct() {
        parent::__construct();
        add_action( 'keyring_importer_twitter_custom_options', array( $this, 'custom_options' ) );
    }

    function custom_options() {
        ?><tr valign="top">
            <th scope="row">
                <label for="include_rts"><?php _e( 'Import retweets', 'keyring' ); ?></label>
            </th>
            <td>
                <input type="checkbox" value="1" name="include_rts" id="include_rts"<?php echo checked( $this->get_option( 'include_rts', true ) ); ?> />
            </td>
        </tr>
        <tr valign="top">
            <th scope="row">
                <label for="include_replies"><?php _e( 'Import @replies', 'keyring' ); ?></label>
            </th>
            <td>
                <input type="checkbox" value="1" name="include_replies" id="include_replies"<?php echo checked( $this->get_option( 'include_replies', true ) ); ?> />
            </td>
        </tr><?php
    }

    function handle_request_options() {
        // Validate options and store them so they can be used in auto-imports
        if ( empty( $_POST['category'] ) || !ctype_digit( $_POST['category'] ) )
            $this->error( __( "Make sure you select a valid category to import your checkins into." ) );

        if ( empty( $_POST['author'] ) || !ctype_digit( $_POST['author'] ) )
            $this->error( __( "You must select an author to assign to all checkins." ) );

        if ( isset( $_POST['auto_import'] ) )
            $_POST['auto_import'] = true;
        else
            $_POST['auto_import'] = false;

        if ( isset( $_POST['include_rts'] ) )
            $_POST['include_rts'] = true;
        else
            $_POST['include_rts'] = false;

        if ( isset( $_POST['include_replies'] ) )
            $_POST['include_replies'] = true;
        else
            $_POST['include_replies'] = false;

        // If there were errors, output them, otherwise store options and start importing
        if ( count( $this->errors ) ) {
            $this->step = 'greet';
        } else {
            $this->set_option( array(
                'category'        => (int) $_POST['category'],
                'tags'            => explode( ',', $_POST['tags'] ),
                'author'          => (int) $_POST['author'],
                'include_replies' => (bool) $_POST['include_replies'],
                'include_rts'     => (bool) $_POST['include_rts'],
                'auto_import'     => (bool) $_POST['auto_import'],
                'user_id'         => $this->service->get_token()->get_meta( 'user_id' ),
            ) );

            $this->step = 'import';
        }
    }

    function build_request_url() {
        // Base request URL
        $url = "http://api.twitter.com/1/statuses/user_timeline.json?";
        $params = array(
            'user_id' => $this->get_option( 'user_id' ),
            'trim_user' => 'true',
            'count' => 75, // More than this and Twitter seems to get flaky
            'include_entities' => 'true',
        );
        if ( false == $this->get_option( 'include_replies' ) )
            $params['exclude_replies'] = 'true';
        if ( true == $this->get_option( 'include_rts' ) )
            $params['include_rts'] = 'true';
        $url = $url . http_build_query( $params );


        if ( $this->auto_import ) {
            // Locate our most recently imported Tweet, and get ones since then
            $latest = get_posts( array(
                'numberposts' => 1,
                'orderby' => 'date',
                'order' => 'DESC',
                'meta_key'    => 'keyring_service', // In case there are other asides
                'meta_value'  => 'twitter',
                'tax_query' => array( array(
                    'taxonomy' => 'post_format',
                    'field' => 'slug',
                    'terms' => array( 'post-format-standard' ), // Tweets stored as asides
                    'operator' => 'IN',
                ) ),
            ) );

            // If we have already imported some, then start since the most recent
            if ( $latest ) {
                $max = get_post_meta( $latest[0]->ID, 'twitter_id', true );
                $url = add_query_arg( 'since_id', $max, $url );
            }
        } else {
            // Handle page offsets (only for non-auto-import requests)
            $url = add_query_arg( 'page', $this->get_option( 'page', 0 ), $url );
        }

        return $url;
    }

    function extract_posts_from_data( $raw ) {
        global $wpdb;

        $importdata = $raw;

        if ( null === $importdata ) {
            $this->finished = true;
            return new Keyring_Error( 'keyring-twitter-importer-failed-download', __( 'Failed to download your tweets from Twitter. Please wait a few minutes and try again.', 'keyring' ) );
        }

        // Check for API overage/errors
        if ( !empty( $importdata->error ) ) {
            $this->finished = true;
            return new Keyring_Error( 'keyring-twitter-importer-throttled', __( 'You have made too many requests to Twitter and have been temporarily blocked. Please try again in 1 hour (duplicate tweets will be skipped).', 'keyring' ) );
        }

        // Make sure we have some tweets to parse
        if ( !is_array( $importdata ) || !count( $importdata ) ) {
            $this->finished = true;
            return;
        }

        // Get the total number of tweets we're importing
        if ( !empty( $importdata[0]->user->statuses_count ) )
            $this->set_option( 'total', $importdata[0]->user->statuses_count );

        // Parse/convert everything to WP post structs
        foreach ( $importdata as $post ) {
            // Double-check for @replies, which shouldn't be included at all if we chose to skip them
            if ( true == $this->get_option( 'exclude_replies' ) && null != $post->in_reply_to_screen_name )
                continue;

            // Post title can be empty for Asides, but it makes them easier to manage if they have *something*
            $title_words = explode( ' ', strip_tags( $post->text ) );
            $post_title  = implode( ' ', array_slice( $title_words, 0, 15 ) );
// Use the first 15 words
            if ( count( $title_words ) > 15 )
                $post_title .= '&hellip;'; 


            // Parse/adjust dates
            $post_date_gmt = strtotime( $post->created_at );
            $post_date_gmt = gmdate( 'Y-m-d H:i:s', $post_date_gmt );
            $post_date     = get_date_from_gmt( $post_date_gmt );

            // Apply selected category
            $post_category = array( $this->get_option( 'category' ) );

            // Clean up content a bit
            $post_content = $post->text;
            $post_content = $wpdb->escape( html_entity_decode( trim( $post_content ) ) );

            // Handle entities supplied by Twitter
            if ( count( $post->entities->urls ) ) {
                foreach ( $post->entities->urls as $url ) {
                    $post_content = str_replace( $url->url, $url->expanded_url, $post_content );
                }
            }

            // Any hashtags used in a tweet will be applied to the Post as tags in WP
            $tags = $this->get_option( 'tags' );
            if ( preg_match_all( '/(^|[(\[\s])#(\w+)/', $post_content, $tag ) )
                $tags = array_merge( $tags, $tag[2] );

            // Add HTML links to URLs, usernames and hashtags
            $post_content = make_clickable( esc_html( $post_content ) );

            // Include geo Data (if provided by Twitter)
            if ( !empty( $post->geo ) && 'point' == strtolower( $post->geo->type ) )
                $geo = array(
                    'lat' => $post->geo->coordinates[0],
                    'long' => $post->geo->coordinates[1]
                );
            else
                $geo = array();

            // Get a GUID from Twitter, plus other important IDs to store in postmeta later
            $user = $this->service->get_token()->get_meta( 'username' );
            $twitter_id              = $post->id_str;
            $twitter_permalink       = "https://twitter.com/{$user}/status/{$twitter_id}";
            $in_reply_to_user_id     = $post->in_reply_to_user_id;
            $in_reply_to_screen_name = $post->in_reply_to_screen_name;
            $in_reply_to_status_id   = $post->in_reply_to_status_id;
            $post_author             = $this->get_option( 'author' );
            $post_status             = 'publish';
            $twitter_raw             = $post;

            // Build the post array, and hang onto it along with the others
            $this->posts[] = compact(
                'post_author',
                'post_date',
                'post_date_gmt',
                'post_content',
                'post_title',
                'post_status',
                'post_category',
                'tags',
                'twitter_id',
                'twitter_permalink',
                'geo',
                'in_reply_to_user_id',
                'in_reply_to_screen_name',
                'in_reply_to_status_id',
                'twitter_raw'
            );
        }
    }

    function insert_posts() {
        global $wpdb;
        $imported = 0;
        $skipped  = 0;
        foreach ( $this->posts as $post ) {
            extract( $post );
            if (
                $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->postmeta} WHERE meta_key = 'twitter_id' AND meta_value = %s", $twitter_id ) )
            ||
                $post_id = post_exists( $post_title, $post_content, $post_date )
            ) {
                // Looks like a duplicate
                $skipped++;
            } else {
                $post_id = wp_insert_post( $post );

                if ( is_wp_error( $post_id ) )
                    return $post_id;

                if ( !$post_id )
                    continue;


                // Track which Keyring service was used
                add_post_meta( $post_id, 'keyring_service', $this->service->get_name() );

                // Store the twitter id, reply ids etc
                add_post_meta( $post_id, 'twitter_id', $twitter_id );
                add_post_meta( $post_id, 'twitter_permalink', $twitter_permalink );
                if ( !empty( $in_reply_to_user_id ) )
                    add_post_meta( $post_id, 'twitter_in_reply_to_user_id', $in_reply_to_user_id );
                if ( !empty( $in_reply_to_screen_name ) )
                    add_post_meta( $post_id, 'twitter_in_reply_to_screen_name', $in_reply_to_screen_name );
                if ( !empty( $in_reply_to_status_id ) )
                    add_post_meta( $post_id, 'twitter_in_reply_to_status_id', $in_reply_to_status_id );

                // Update Category and Tags
                wp_set_post_categories( $post_id, $post_category );
                if ( count( $tags ) )
                    wp_set_post_terms( $post_id, implode( ',', $tags ) );

                // Store geodata if it's available
                if ( !empty( $geo ) ) {
                    add_post_meta( $post_id, 'geo_latitude', $geo['lat'] );
                    add_post_meta( $post_id, 'geo_longitude', $geo['long'] );
                    add_post_meta( $post_id, 'geo_public', 1 );
                }

                add_post_meta( $post_id, 'raw_import_data', json_encode( $twitter_raw ) );

                $imported++;
            }
        }
        $this->posts = array();

        // Return, so that the handler can output info (or update DB, or whatever)
        return array( 'imported' => $imported, 'skipped' => $skipped );
    }
}

} // end function Keyring_Twitter_Importer


add_action( 'init', function() {
    Keyring_Twitter_Importer(); // Load the class code from above
    keyring_register_importer(
        'twitter',
        'Keyring_Twitter_Importer',
        plugin_basename( __FILE__ ),
        __( 'Import all of your tweets from Twitter as Posts (marked as "asides") in WordPress.', 'keyring' )
    );
} );
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

In the insert_posts function, before the line $post_id = wp_insert_post( $post ); add the following (and suit to your needs):

Link to Twitter User Page

$post['post_content'] .= '<a href="http://twitter.com/__USERNAME__">'
    .'Follow me on Twitter'
    .'</a>';

and/or

Link to Twitter Status

$post['post_content'] .= '<a href="'.$post['twitter_permalink'].'">'
    .'View this post on Twitter'
    .'</a>';
share|improve this answer
    
Thanks for the reply. I've just tried this and it's come up throwing this error: Warning: Attempt to assign property of non-object in /home/devrjtho/public_html/wp-content/plugins/keyring-social-importers/importers‌​/keyring-importer-twitter.php on line 248. Any idea why? –  user2185625 Apr 10 '13 at 11:30
    
That seems to work now. Thanks for your help! As a side question, to display an Follow button, would this work the same way? –  user2185625 Apr 10 '13 at 12:28
    
Well, $post['post_content'] is a string containing the post's plain text and (allowed) HTML tags. If you want to append a link (that's what we did so far), just concatenate (.=) the current content and the particular HTML code. If you want to append an image, you go the same way. –  t f Apr 10 '13 at 12:35
    
Good stuff. I'll give it a go. One final question... any idea how I could post the actual URL to the Tweet after the tweet? For example..."Tweet tweet tweet<BR>View this post on Twitter (with a link to the actual status itself on Twitter)" –  user2185625 Apr 10 '13 at 12:52
    
You already have the URL. It's stored in $post['twitter_permalink']. Meaning: if you want to append that, do something like $post['post_content'] .= '<a href="'.$post['twitter_permalink'].'">View this post on Twitter</a>'; –  t f Apr 10 '13 at 12:58
show 1 more 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.