WordPress.org

Ready to get started?Download WordPress

Forums

Live Blogging
[resolved] [Live Blogging] Add hashtag to outgoing tweet (4 posts)

  1. Hi. I've been live blogging WordCamp Philly using the Live Blogging plugin from Chris Northwood (who is no longer actively developing it). I want to add to the following code the ability to pull a custom field entitled "hashtag" from the original post and append it to the end of the tweet.

    Does anybody know how I would go about this? I searched on the codex and tried to add the code based on what I found there, but it returned errors.

    add_action('publish_liveblog_entry', 'live_blogging_tweet', 10, 2);
        function live_blogging_tweet($id, $post)
        {
            if ('1' == get_option('liveblogging_enable_twitter') && '' == get_post_meta($id, '_liveblogging_tweeted', true))
            {
                $connection = new TwitterOAuth(LIVE_BLOGGING_TWITTER_CONSUMER_KEY, LIVE_BLOGGING_TWITTER_CONSUMER_SECRET, get_option('liveblogging_twitter_token'), get_option('liveblogging_twitter_secret'));
                $content = filter_var($post->post_content, FILTER_SANITIZE_STRING);
                if (strlen($content) > 140)
                {
                    $tweet = $connection->post('statuses/update', array('status' => substr($content, 0, 139) . html_entity_decode('…', ENT_COMPAT, 'UTF-8')));
                }
                else
                {
                    $tweet = $connection->post('statuses/update', array('status' => $content));
                }
            }
            if (isset($tweet->id))
            {
                update_post_meta($id, '_liveblogging_tweeted', $tweet->id);
            }
        }

    http://wordpress.org/extend/plugins/live-blogging/

  2. chrisnorthwood
    Member
    Plugin Author

    Posted 2 years ago #

    Hi Andrew,

    I can't remember the syntax off the top of my head for getting the ID of the parent post, but what you probably want to do is something like this, replacing after the $content = line:


    $custom = get_post_custom($parent_post_id);
    $hashtag = ' ' . $custom['hashtag'];
    if (strlen($content) > 140 - strlen($hashtag))
    {
    $tweet = $connection->post('statuses/update', array('status' => substr($content, 0, 139 - strlen($hashtag)) . html_entity_decode('…', ENT_COMPAT, 'UTF-8') . $hashtag));
    }
    else
    {
    $tweet = $connection->post('statuses/update', array('status' => $content . $hashtag));
    }

    If anyone gets the code sorted, send me a patch and I'll merge it in and release it in the next version of Live Blogging

  3. Thanks, that gave us the direction we needed @jason_coleman came up with most of the below code that works, and @kcristiano at #wcphilly helped me debug and edit it. Here's the fixed function beginning at line 1157.

    add_action('publish_liveblog_entry', 'live_blogging_tweet', 10, 2);
        function live_blogging_tweet($id, $post)
        {
            if ('1' == get_option('liveblogging_enable_twitter') && '' == get_post_meta($id, '_liveblogging_tweeted', true))
            {
                $connection = new TwitterOAuth(LIVE_BLOGGING_TWITTER_CONSUMER_KEY, LIVE_BLOGGING_TWITTER_CONSUMER_SECRET, get_option('liveblogging_twitter_token'), get_option('liveblogging_twitter_secret'));
                $content = filter_var($post->post_content, FILTER_SANITIZE_STRING);
    
                //get the hashtag (we need the id of the parent blog post, not $post->ID here)
    
                $parent_post_id = (int)$_POST['live_blogging_entry_post'];
                $hashtag = get_post_meta($parent_post_id, "liveblogging_hashtag", true);
    
                //how much space do we have?
                if($hashtag)
                    $tweetlength = 140 - strlen($hashtag) - 2;
                else
                    $tweetlength = 140;
    
                //do I need to trim the tweet?
                if (strlen($content) > $tweetlength)
                {
                    $content = substr($content, 0, $tweetlength - 1) . html_entity_decode('…', ENT_COMPAT, 'UTF-8');
                }
    
                //add hashtag
                if($hashtag)
                    $content .= " #" . $hashtag;
    
                //send tweet
                $tweet = $connection->post('statuses/update', array('status' => $content));
            }
            if (isset($tweet->id))
            {
                update_post_meta($id, '_liveblogging_tweeted', $tweet->id);
            }
        }

    It'd also be a good idea to have the user enter their own twitter_consumer_key and twitter_consumer_secret, rather than having your own hard coded in. That's actually something I think I know how to do, so I'll work on that and then post here if/when I get it done. Alex King's Twitter Tools already has an interface for that, so I'll be able to use that as a reference.

  4. chrisnorthwood
    Member
    Plugin Author

    Posted 2 years ago #

    Hi Andrew,

    Thanks for this patch. I've now included this and released it as version 2.2.1.

    Chris

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic