i just want this code to be added in my functions.php, so that it will directly display after my post ends currently i am adding this code to my single.php, but i want to add this is in functions.php, this code is use to fetch all the tweets of the respective account, the code is here

function parse_twitter_feed($feed, $prefix, $tweetprefix, $tweetsuffix, $suffix) {
  $feed = str_replace("&lt;", "<", $feed);
  $feed = str_replace("&gt;", ">", $feed);
  $clean = explode("<content type=\"html\">", $feed);

  $amount = count($clean) - 1;

  echo $prefix;

  for ($i = 1; $i <= $amount; $i++) {
    $cleaner = explode("</content>", $clean[$i]);
    echo $tweetprefix;
    echo $cleaner[0];
    echo $tweetsuffix;

  echo $suffix;

function the_twitter_feed($username) {
  // $username = "Mba_"; // Your twitter username.
  $limit = "5"; // Number of tweets to pull in.

  /* These prefixes and suffixes will display before and after the entire block of tweets. */
  $prefix = ""; // Prefix - some text you want displayed before all your tweets.
  $suffix = ""; // Suffix - some text you want displayed after all your tweets.
  $tweetprefix = ""; // Tweet Prefix - some text you want displayed before each tweet.
  $tweetsuffix = "<br>"; // Tweet Suffix - some text you want displayed after each tweet.

  $feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=" . $limit;

  $twitterFeed = get_transient($feed);
  if (!$twitterFeed) {
    $twitterFeed = wp_remote_fopen($feed);
    set_transient($feed, $twitterFeed, 3600); // cache for an hour
  if ($twitterFeed)
    parse_feed($twitterFeed, $prefix, $tweetprefix, $tweetsuffix, $suffix);
  • 2
    ok, and what is your question?
    – tkit
    Dec 11, 2010 at 11:23
  • when i add this code to my functions.php, den it instead of my website, it loads this function, i just want this code to be added in such a way that it comes directly after my post ends Dec 11, 2010 at 11:28
  • In that case you should add it in your template. Dec 11, 2010 at 12:00

2 Answers 2


I'm not sure why you don't just insert the code in the single.php, or use Denis's solution, but if you want to hook into the_content you can do so by putting the following in your functions.php file:

function append_the_content($content) {
    $content .= 'PUT YOUR FUNCTION HERE';
       return $content;
add_filter('the_content', 'append_the_content');

This will add directly to the end of the_content.

You can call your Twitter function above this and it should work. You'd be better off using a theme framework with some custom hooks because hooking into the_content in this way can get very buggy very fast depending on what other filters/hooks your theme and plugins are using to modify the_content. I don't know why it happens, I just know that it does.

  • the code which you provided, where i have to copy paste my code which is at the top, in $content.=my code or before your code ????? Dec 12, 2010 at 5:35
  • Put your functions before this function in your functions.php. Then replace 'PUT YOUR FUNCTION HERE' with the_twitter_feed('username'). You can test it by just pasting the function and not changing anything. Your template page should show PUT YOUR FUNCTION HERE right after your content. If this shows but things get messed up when you replace that with your function it means that there some other plugin or theme hook that's interfering with your $content hook. I tested this on two of my sites. On one it worked fine, the other it put the function up in the header and I don't know why.
    – matt
    Dec 12, 2010 at 7:04
  • hey when i tried the above code i am getting this error Fatal error: Call to undefined function parse_feed() in wp-content\themes\cityguide\functions.php on line 64 Dec 13, 2010 at 5:30
  • It's because wordpress is executing your the_twitter_feed($username) function which calls the parse_feed() function before it is defined. You need to make sure that a function is defined before you call it which is easy when it's all in the same file, but I'm not sure where the parse_feed() function is defined so I can't tell you specifically what to change. If its in a seperate file then it has to do with the order in which wordpress initiates different parts of the site and I can't help you much with that because it's a little out of my league.
    – matt
    Dec 13, 2010 at 12:22

Using FTP, grab your theme's functions.php, and add the above code within it, minus the leading <?php and the trailing ?>, towards the latter file's end (before the ?>, which terminates php).

I've edited your function so it can then be used in a template:

<?php the_twitter_feed('username'); ?>
  • i tried that, but it is giving just the output of this function file, not the website Dec 11, 2010 at 11:26
  • Are you sure you removed the <?php and ?> parts from the above code, and placed the resulting code within the functions.php file's <?php and ?> parts? Dec 11, 2010 at 11:42
  • yes, yes i removed it, and then i inserted the code in that file Dec 11, 2010 at 11:59
  • 1
    @Denis see bottom of the code, this is not just a function - it runs and echoes stuff. PS no caching, no WP APIs used where they should be... Twitter inspires some terrible code as usual. :(
    – Rarst
    Dec 11, 2010 at 12:01
  • 1
    In the future, that said, I suspect that elance.com or similar sites, or outright hiring someone you trust, might be better venues for this kind of question. Dec 11, 2010 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.