This morning, I was reading an interesting article about plugins in WordPress, and how to get rid of them (available here, in French… Google Translate is your friend 😉 ).
I wanted to test adding my Tweet feed, not impacting performance (I tested several plugins already, but they decreased performance, as I could measure on GTmetrix).
And this hack works !
Here is what I did:
- Add a plugin to allow php code in Text widget
- Add code in functions.php file from my template
- Add a widget to display result in the sidebar
First I had to install a plugin, because I wanted to use Widgets and this hack needs to add HTML + PHP code to work.
So I installed the plugin PHP Text Widget.
Second, I copied/pasted the following source code (from somix blog) in my theme functions.php page (Menu Appearance -> Editor -> Theme Functions).
I added it at the end.
function twitterstatut() { ob_start(); //Params $username = "username"; //You Tweeter account $limit = "3";//How many Tweet to display $tweetprefix = "<li>"; //Before each tweet $tweetsuffix = "</li>"; //After each tweet //Fetching the feed $feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=" . $limit; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $feed); $twitterFeed = curl_exec($ch); curl_close($ch); //Feed cleanup $twitterFeed = str_replace("<", "<", $twitterFeed); $twitterFeed = str_replace(">", ">", $twitterFeed); $twitterFeed = str_replace(""", "\"", $twitterFeed); $twitterFeed = str_replace("&apos;", "'", $twitterFeed); $twitterFeed = str_replace("&", "&", $twitterFeed); $clean = explode("<content type=\"html\">", $twitterFeed); $cleandate = explode("<published>", $twitterFeed); $amount = count($clean) - 1; $amount = count($cleandate) - 1; //Feed display for ($i = 1; $i <= $amount; $i ) { $cleaner = explode("</content>", $clean[$i]); $cleanerbis = explode("</published>", $cleandate[$i]); $mydate = strtotime($cleanerbis[0]); $time_diff = time() - $mydate; if ( $time_diff > 0 )$display = sprintf( __('%s ago'), human_time_diff( $mydate ) ); $rendu = $tweetprefix.$cleaner[0].'<span class="small"> ['.$display.']</span>'.$tweetsuffix; echo $rendu;} return ob_get_clean();} add_option('cachetwithtml','0','','yes'); add_option('cachetwittimer',mktime() - 10000,'','yes'); function cachetwit() { $time3 = mktime(); if ( $time3 > get_option('cachetwittimer') + 1800 ) { $myvar = twitterstatut(); update_option('cachetwittimer', mktime()); if ( $myvar == '') {} else {update_option('cachetwithtml', $myvar);}} echo get_option('cachetwithtml');}
Just need to change:
- your username (row #4)
- how many tweets do you want to display (row #5)
- your CSS style to use (row #32)
- delay to cache this information (row #40 : 1800s in my case)
This last parameter is interesting, as it highlights the cache mechanism in place. This is probably the reason why I didn’t see any impact on performance once deployed….
Last action: create a Text widget in the sidebar (Menu Appearance -> Widgets), and copy/paste the following code:
<?php if (function_exists('cachetwit')) {cachetwit();}?>
It simply works !