Automatically post to twitter in Wordpress

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PTBKoo
Forum Newbie
Posts: 1
Joined: Sat Jan 02, 2010 1:33 pm

Automatically post to twitter in Wordpress

Post by PTBKoo »

Hello, I was wondering how you would use the code for wordpress below to automatically post to twitter. I added the function below in custom functions and it worked great. The problem is I want it to use bit.ly shortener and for that I need to use the code:

Code: Select all

<?php echo $burl; ?>
As you can see the code to get a shortened url uses <?php ?>
When I add

Code: Select all

function twitterUpdate($postTitle, $postLink, $isNew) 
{
?>post to twitter code below<?php
the code becomes plain text instead of code. I remember reading a post here about using curl & using the php code but for the life of me I cant find it anywhere.

How would I add it to the code below so it works correctly?

Code: Select all

function twitterUpdate($postTitle, $postLink, $isNew)
{
    // Enter Your Twitter ID Here
    $username = 'Twitter_ID';
    // Enter Your Twitter Password Here
    $password = 'Password';
 
        # text into a twitter friendly text
    $code_entities_match = array('--','"','!','@','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','.','/','*','+','~','`','=');
    $code_entities_replace = array('-','','','','','','','','','','','','','','','','','','','','','','','','');
    $postTitle = str_replace($code_entities_match, $code_entities_replace, $postTitle);
 
    // Check if New or Updated Post
    if($isNew)
        $postTitle = 'New Post: ' . $postTitle;
    else
        $postTitle = 'Updated Post: ' . $postTitle;
 
    // Calculate Twitter Msg and keep it under 140 Chars
        if(strlen ($postTitle) > (140 - strlen ($postLink)))
        $postTitle      = substr_replace($postTitle, '...', (140 - 3 - strlen ($postLink)));
 
    $message = $postTitle . $postLink;
 
    // The twitter API address
    $url = 'http://twitter.com/statuses/update.xml';
    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, "$url");
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_POST, 1);
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
    curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
    $buffer = curl_exec($curl_handle);
    curl_close($curl_handle);
 
    // Uncomment the lines below to check if
    // CURL is enabled on your Web Server
    // check for success or failure
    /*
    if (empty($buffer))
        echo 'message';
    else
        echo 'success';
    */
}

Code: Select all

function postToTwitter($post_ID)
{
    // Create your Short URL replace with your blog url
    $postLink = ' http://clazh.com/?p=' . $post_ID;
    // encode the URL to fix Post to Twitter issues
        $postLink = urlencode  ( $postLink ) ;
    // Get the Post Object
    $get_post_info  = get_post( $post_ID );
    // Get the Post Title
    $postTitle = $get_post_info->post_title;
        // Get the Post date
    $postDate       = date('U', strtotime($get_post_info->post_date));
    // Get the post Modified date
    $postModified   = date('U', strtotime($get_post_info->post_modified));
 
    // Check if the post is new or modified
    if($postModified == $postDate)
    {
        twitterUpdate($postTitle, $postLink, true);
    }
    /*  If You want to fire everytime You update
          a post remove these comments around the else.
    else
    {
        twitterUpdate($postTitle, $postLink, false);
    }
    */
}
 
// Post to twitter when you publish or update a post
add_action('publish_post', 'postToTwitter');
Post Reply