Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

<?php
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);
}
?>
share|improve this question
2  
ok, and what is your question? –  pootzko Dec 11 '10 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 –  ntechi Dec 11 '10 at 11:28
    
In that case you should add it in your template. –  Denis de Bernardy Dec 11 '10 at 12:00

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
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 ????? –  ntechi Dec 12 '10 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 '10 at 7:04
    
ok thanks, for your help –  ntechi Dec 12 '10 at 7:50
    
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 –  ntechi Dec 13 '10 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 '10 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'); ?>
share|improve this answer
    
i tried that, but it is giving just the output of this function file, not the website –  ntechi Dec 11 '10 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? –  Denis de Bernardy Dec 11 '10 at 11:42
    
yes, yes i removed it, and then i inserted the code in that file –  ntechi Dec 11 '10 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 '10 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. –  Denis de Bernardy Dec 11 '10 at 13:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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