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 asked this question previously, which solved my problem, but further i am having still problem with it,

i have a function to display tweets of a user which fetch from a custom field, the custom field code is working perfect, cause when i echo the get_post_meta in my single.php i get the value which is input

now the question is when i try to fetch the value from custom field to a twitter function the tweets are not coming,

here is my code:

function tweet(){

$doc = new DOMDocument();

if ( $meta =get_post_meta($post->ID, 'dbt_Username', true) ) { 
$feed = "http://twitter.com/statuses/user_timeline/$meta.rss"; 
$doc->load($feed); 
  $outer = "<ul>";
  $max_tweets = 5;    

  $i = 1;
  foreach ($doc->getElementsByTagName('item') as $node) {
    $tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;


    $tweet = substr($tweet, stripos($tweet, ':') + 1);   


    $tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', 
          '<a href="$1">$1</a>', $tweet);


    $tweet = preg_replace("/@([0-9a-zA-Z]+)/", 
          "<a href=\"http://twitter.com/$1\">@$1</a>", 
          $tweet);

    $outer .= "Twiter Updates by:".get_option('tweetID')."<li>". $tweet  . "</li>\n";


    if($i++ >= $max_tweets) break;
  }
   $outer .= "</ul>\n";
  return $outer;
  }
}

add_action('admin_menu', 'tweet_fetch1');

function append_the_content1($content) {
     $content .= "<div class='post'><p>".tweet(get_post_meta($post->ID, 'dbt_Username', true))."</p></div>";
      return $content;
      }
add_filter('the_content', 'append_the_content1');
share|improve this question
    
What happens when you put <?php echo tweet(get_post_meta($post->ID, 'dbt_Username', true)); ?> directly into your single.php file? Does it work in that case? It seems to me that if it doesn't show in that case then it's a problem with that function. If it works, then it's a problem with the other function or the way you are calling it. –  matt Dec 18 '10 at 6:04
    
if i am just writiing <?php echo tweet(get_post_meta($post->ID, 'dbt_Username', true)); ?> then no div is coming and no tweets, if also i write only <?php tweet(); ?> then also nothin comes except the post i also think that the problem is with the function –  ntechi Dec 18 '10 at 6:39
add comment

1 Answer

up vote 2 down vote accepted

So many issues here...

First, I'm not sure what this line is doing:

add_action('admin_menu', 'tweet_fetch1');

If you don't actually have a function called tweet_fetch1 that runs when the admin loads, you should remove this add_action. And if you do have such a function, why is your add_action in this location? The first step to avoiding problems is clean, readable, commented code the groups functions and hooks them logically.

Second, in both of your functions, you're trying to reference $post->ID where no post object is available. You can usually make it available by globalizing it.

global $post;

Third, your function called "tweet" accepts no arguments, yet when you call it from inside your append_the_content1() function, you're trying to pass it the meta value for dbt_Username.

$content .= "<div class='post'><p>".tweet(get_post_meta($post->ID, 'dbt_Username', true))."</p></div>";

This is incorrect...and unnecessary, as you then grab the same meta value (again) from inside the tweet() function using get_post_meta. You need to remove the argument (the stuff between the parentheses) when you call the tweet() funciton. Like so:

$content .= "<div class='post'><p>".tweet()."</p></div>";

Lastly--though this is just a suggestion, and isn't strictly something wrong with your code--I don't know what all that preg replacing is for. If you're trying to link to the original tweet, you can pull the URI right out of the twitter feed. Also, you can make links inside tweets clickable with WordPress' handy make_clickable() function.

Your problem here isn't with get_post_meta--it's with PHP. No disrespect, but it would help you out tremendously to brush up on the basics so that you can better understand the code you're writing, and the answers people are giving you.

share|improve this answer
    
ok i understand this, first add_action('admin_menu', 'tweet_fetch1'); this line is of another function which by mistakingly i copied, global $post i ll try it now, and i didnt understand your third step, what arguments to pass to function, the custom fields which i created is also through a function, i wrote a function on it to display it in edit post page, from there i am getting the post_meta, so what changes i have to do in third step –  ntechi Dec 18 '10 at 7:31
    
hey second and the happening thing, after putting global $post; it worked the tweets are coming, thanks a lot mate for your help –  ntechi Dec 18 '10 at 7:34
2  
Again, this is about you not understanding php. If you're defining a function that accepts an argument you do it like: function my_function($my_argument){//function code}. Your tweet function, however, is defined like: function tweet(){}, meaning it accepts no args. So when you call it, you can only call it like: tweet(), not like: tweet($something). I'm updating my answer to make this more clear. –  MathSmath Dec 18 '10 at 7:36
add comment

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.