5

I'm working on a website with twitter share option for each specific product.

I followed twitter API instructions for tweet-sharing, and everything works fine except custom display of text. For example I want user to tweet like this: "What do you think? Should I buy this? http://url.etc @mywebsite"

but all I get when user tweets is the link: http://url.etc

This is the code:

<script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
<a target="_blank" href="https://twitter.com/share" data-url="http://bit.ly/twitter-api-announce" data-via="testtest" data-text="What do you think? Should I buy this? " data-count="none" data-counturl="http://groups.google.com/group/twitter-api-announce" >TWITTER</a>

The problems seems to be with data-text option.

Any experience on this? Ideas? Thanks

Slavisa Perisic
  • 1,110
  • 3
  • 18
  • 32

5 Answers5

6

On Wordpress I just used <a href="https://twitter.com/share?url=google.com&text=your text here">Tweet</a>

Works like a charm!

lostkat
  • 61
  • 1
  • 2
4

simply use a link like :

<a href="http://twitter.com/share?related=[your_twitter_account]&via=[your_twitter_account]&lang=[fr]&text=[hello%20world]&url=[www.google.com]">tweet</a>

Just change what is between [] (and remove them) note that everything have to be RFC (with weird chars such as 'space' replaced by %20 etc.)

twitter propose a nice page to make the buttons https://about.twitter.com/resources/buttons#tweet But my solution avoid the javascript to force the design of the buton

Fenix Aoras
  • 221
  • 3
  • 10
1

You can use this:

<a href='https://twitter.com/share?url=google.com&text=Signup>Tweet</a <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src='//platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document,'script','twitter-wjs');</script>  
Shankar ARUL
  • 12,642
  • 11
  • 68
  • 69
0

Confirm that you have included the correct twitter scripts . Better still, generate your tweet button code from the twitter developer interface here .. https://about.twitter.com/resources/buttons

If you would like to modify the tweetbutton content on the fly ...e.g after page load, you will have to creat and insert your tweet button into the html DOM dynamically .

Some guidance on that can be found here .

http://denvycom.com/blog/twitter-button-with-dynamic-custom-data-text-message/

Hopefully this is helpful.

Vykthur D .
  • 351
  • 2
  • 7
0

You have to encode your text before inserting it in the link. The correct procedure is:

  1. Encode the text with an online tool like this one

  2. Put the result inside an HTML link (as suggested by @FenixAoras): <a href="http://twitter.com/share?related=[your_twitter_account]&via=[your_twitter_account]&lang=[fr]&text=[encoded-text]&url=[url]">Share on Twitter</a>


If you generate your HTML with php, you can use the urlencode function directly in your script:

echo "<a href="http://twitter.com/share?text=".urlencode("<your text>")."">Share on Twitter</a>";

If you are using WordPress, the best way is creating a shortcode, beacuse with it you can use also the native functions of WordPress:

function tweet_this($atts, $content = null)
{
    extract(shortcode_atts(array(
        "text" => ''
    ), $atts));
    return "<a href='https://twitter.com/intent/tweet?text=".urlencode( $text." - ".get_the_title()." - ".get_permalink() )."'>Share on Twitter</a>";
}
add_shortcode( 'tweet_this', 'tweet_this' );

(Note: the code above is just a lead, you can expand it and you have to test)

Usage:

[tweet_this text="my custom text with #hashtag and @Mention"]
Marco Panichi
  • 1,068
  • 1
  • 18
  • 31