Reputation: 339
I'm trying to implement a 'Tweet' button on my wordpress site and am having an issue where the URL doesn't show in the tweet.
It shows the hashtag and the account but not the URL.
My code is as follows:
<a href="https://twitter.com/share" class="twitter-share-button" data-url="<?php echo urlencode(get_permalink($post->ID)); ?>" data-via="mixtapes_4_free" data-related="realdannys" data-hashtags="mixtape">Tweet</a>
If i don't use the data-url
then it just inserts the base domain name and not the full URL.
Any ideas?
Upvotes: 1
Views: 3841
Reputation: 11
I had the following code:
<a href="<?php echo $tweet_url; ?>" class="twitter-share-button" data-url="=<?php the_permalink(); ?>" data-via="<?php echo of_get_option( 'ac_twitter_username' ); ?>" data-lang="en" data-related="anywhereTheJavascriptAPI" data-count="vertical">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
But that didn't work, so I tried a simplified version:
<a href="http://twitter.com/share?url=<?php echo urlencode(wp_get_shortlink()); ?>&counturl=<?php urlencode(the_permalink()); ?>" class="twitter-share-button" data-count="vertical" data-via="<?php echo of_get_option( 'ac_twitter_username' ); ?>">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
And this one worked just fine. Just keep in mind that the username has to be somehow integrated in your theme settings, if it's not, you can change:
<?php echo of_get_option( 'ac_twitter_username' ); ?>
For your twitter username.
I hope this works, It took me a while to find this more generic approach. I don't fully know what I'm doing, so if anyone sees an issue with this approach, please let me know. So far, it works great.
Thanks!
Upvotes: 1
Reputation: 6359
You are missing javascript code.
You probably got your code from HERE
For sharing a link, the javascript code is
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
Just add this code after your anchor tag.
Upvotes: 1