We can integrate Twitter Hashtag, @ and Inline Tweet in WordPress in several ways. It matters how you want to use the features in your blog. With time, Twitter API has undergone a huge change, possibly you will want to read about hashtag system, Twitter Hashtag and Twitter Tricks and Cheat Sheet With New API guide to get used with Twitter specific stuffs. Plugins are there, we will definitely mention but one must know the existence of non-WordPress Plugin ways to integration. Essentially, using lot of plugins can make your database quite pathetic. There are some “paid Plugins” too, although buying such is not recommended as they will try to use some API to authenticate your item which might compromise your security.
Twitter Hashtag, @ and Inline Tweet in WordPress : Let Us Start With Easiest Example
We can not use @anywhere platform
anymore, its old and retired. With PHP preg replace, we can simply do many works :
1 2 3 4 5 6 7 8 9 | // Display Twitter @username links function tweet($text) { $text = preg_replace('/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', "$1<a href=\"https://twitter.com/$2\" class=\"tweet-username\">@$2</a>$3 ", $text); $text = preg_replace('/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)#{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', "$1<a href=\"https://search.twitter.com/search?q=%23$2\" class=\"tweet-hashtag\">#$2</a>$3 ", $text); return $text; } // Replace content and excerpt add_filter('the_content', 'tweet', 9); add_filter('the_excerpt', 'tweet', 9); |
If we sanitize and add it on current Theme or Child Theme’s functions.php
file, it will automatically link from text. But this is a hardcoded HTML link towards Twitter which you might not link. Possibly you know that, if @AbhishekCTRL is the username, we can reach it via hashtag too. These are same links :
---
1 2 3 | https://twitter.com/#!/AbhishekCTRL # equivalent to https://twitter.com/AbhishekCTRL |
Now, how we can create an inline Twitter Retweet Button, this is called Twitter Intent :
1 | https://dev.twitter.com/docs/intents#retweet-intent |
If you manually want to add a RT button, only inserting this after modification is enough :
1 2 3 | <a href="https://twitter.com/intent/retweet?tweet_id=504205447910281216" class="retweet" style="display:inline-block;font-family:georgia,serif;font-size:12px;color:#000;text-decoration:none;padding:1px 5px;border:1px solid #ccc;border-radius:3px;background-color:#ddd;background:linear-gradient(to bottom, #f6f6f6, #ddd)">Retweet</a> <style>.retweet:hover{opacity:0.9}</style> <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-wks");</script> |
Here is the thing when I pasted in text mode in WordPress :
Indeed, I used only :
1 2 | <a href="https://twitter.com/intent/retweet?tweet_id=504205447910281216" class="retweet" style="display:inline-block;font-family:georgia,serif;font-size:12px;color:#000;text-decoration:none;padding:1px 5px;border:1px solid #ccc;border-radius:3px;background-color:#ddd;background:linear-gradient(to bottom, #f6f6f6, #ddd)">Retweet</a> <style>.retweet:hover{opacity:0.9}</style> |
The Javascript is already loaded by the social sharing button. By the way, I grabbed the ID from here:
1 | https://twitter.com/AbhishekCTRL/status/504205447910281216 |
Now, we can modify and exclude all css style – Retweet – click it, it will give you a nice Javascript popup! Quite easy code :
1 | <a href="https://twitter.com/intent/retweet?tweet_id=504205447910281216" class="retweet">Retweet</a> |
As the class="retweet"
is there, it will work. Unless you are a fool, you will not use a Twitter Plugin for these simple usages. Oho, understood, you are not liking RT to the same stuff, you want a sentence to included to RT, right? You can retweet our Install WordPress with Nginx on Rackspace Cloud Server guide! Do it!. I retrieved it via an old post. Anyway, here is the thing :
1 | <a href="https://twitter.com/intent/tweet?original_referer=https%3A%2F%2Fthecustomizewindows.com%2F2014%2F08%2Finstall-wordpress-nginx-rackspace-cloud-server%2F&text=Install%20WordPress%20with%20Nginx%20on%20Rackspace%20Cloud%20Server&tw_p=tweetbutton&url=http%3A%2F%2Fbit.ly%2F1sLQugH&via=AbhishekCTRL" class="retweet">You can retweet our Install WordPress with Nginx on Rackspace Cloud Server guide! Do it!</a> |
Very easy. Look at this broken explanation :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # constant <a href="https://twitter.com/intent/tweet? # original referrer is the safe url of the post original_referer=https%3A%2F%2Fthecustomizewindows.com%2F2014%2F08%2Finstall-wordpress-nginx-rackspace-cloud-server%2F # &text is what will be shared &text=Install%20WordPress%20with%20Nginx%20on%20Rackspace%20Cloud%20Server # source is tweet button, its a hack &tw_p=tweetbutton # this is url shortener's url &url=http%3A%2F%2Fbit.ly%2F1sLQugH # via is me &via=AbhishekCTRL" # class is to force the javascript class="retweet"> # my custom text You can retweet our Install WordPress with Nginx on Rackspace Cloud Server guide! Do it! # ended the hyperlink </a> |
The complex looking url is nothing but encoded url, you can read Percent-encoding and URL Encode/Decode to know more. You can retrieve an url in WordPress using the_permalink()
function. That will make it automated. I will make it better in near future in fully handed way without using any plugin. I guess you have no complain now. Hashtag will get auto linked with that PHP script if you want. It is more easy – just a hyperlink, in case you want to manually do to prevent unwanted stuffs.
Twitter Hashtag, @ and Inline Tweet in WordPress : Middle With Javascript
In case you want auto link via Javascript. There is also method. First create a Twitter Application with your domain name – do not install those stuffs, only create the Twitter Application. You will get an API. Load this Javascript at header :
1 2 | <a href="https://twitter.com/share" class="twitter-share-button" data-via="AbhishekCTRL" data-related="AbhishekCTRL" data-hashtags="AbhishekGhosh">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-wks');</script> |
Unfortunately this automatic thing has limitations, despite it can work on all pages (no need to bother about the url). In action :
Disadvantage is that, we can not reuse it for linking as many as we will love, plus if the person hits backspace, the session will get terminated.
Twitter Hashtag, @ and Inline Tweet in WordPress : End With Complicated Plugins
There are some plugins :
1 2 | http://wordpress.org/plugins/evergreen-post-tweeter/ http://wordpress.org/plugins/twitter-tools/ |
Pure HTML, No Javascript, With Icon Font Twitter Hashtag, @ and Inline Tweet in WordPress
Here is a real working example :
Tweet this real working link .
We used this HTML :
1 | <a class="twitter" href="http://twitter.com/home/?status=Twitter Hashtag, @ and Inline Tweet in WordPress https://thecustomizewindows.com/2014/08/integrate-twitter-hashtag-and-inline-tweet-in-wordpress/ by @AbhishekCTRL" target="_blank" title="Click to tweet this">Tweet this real working link <i class="icon-twitter"></i></a> |
As icon-font is already loaded on this webpage with CSS for other Twitter icon, we need not to load anything more. You can perform search on our website with related keywords for any required helpful in-depth guide.
Tagged With include hashtags in wordpress excerpt , inline tweet , wordpress twitter hashtag