I'm hacking into a wordpress plugin. Since I'm inside a plugin that posts tweets, I use something like echo or print. I've identified one line that causes the problem, but not sure what exactly it is, since I can't print or echo.
$tagarray = wp_get_post_tags( $post_id); //returns an array
$tags = "tag:";
if (count($tagarray) > 0){
foreach ( $tagarray as $tag ){
//$tags .= "mytag "; // This line works when quo开发者_运维问答tes are removed.
//$tags = strval($tag); //This doesn't.
}
}
I've tried various manipulations of $tag, like casting etc, but nothing works. Any ideas on how to debug? My last resort would be to publish print values to a file. Any other ideas?
Your code is incomplete.
Yes, wp_get_post_tags()
returns a list of tags, but each tag in that list is itself an object containing the following:
- term_id
- name
- slug
- term_group
- term_taxonomy_id
- taxonomy
- description
- parent
- count
So you need to do this instead:
$tagarray = wp_get_post_tags( $post_id ); //returns an array
$tags = "tag:";
if (count($tagarray) > 0){
foreach ( $tagarray as $tag ){
$tags .= strval( $tag->name );
}
}
In you're loop use :
var_dump($tag);
die();
You should be able to see exactly what type of is $tag and what content . the loop will stop imediatly after the first value .
I find FirePHP (addon for FireBug/Firefox) very useful for debugging php, you can send messages, arrays, variables to the firebug console before headers are sent.
for debugging php I use PhpED you can set breakpoints in the code without any modifications, so there is no chance that you'll leave some echos in the code.
for debugging use var_dump or print_r
that doesn't work because you missed to concatation operator (.) in the second line
$tagarray = wp_get_post_tags( $post_id); //returns an array
$tags = "tag:";
if (count($tagarray) > 0){
foreach ( $tagarray as $tag ){
//$tags .= "mytag "; // This line works when quotes are removed.
$tags .= strval($tag); //This will work Changed = to .=
}
}
And if are looking for debugging the code FirePHP is a good tool, also you could use any PHP IDE with builtin debugging like eclipse or something.
And in wordpress you can use WP-Debug Plugin
To debug, since it won't output any sort of debugging info to the browser, do this, or something similar:
$logger = fopen('debug.log','a');
fwrite($logger,"Begin Debug - " . time() . "\n------------\n");
$tagarray = wp_get_post_tags( $post_id); //returns an array
$tags = "tag:";
if (count($tagarray) > 0){
foreach ( $tagarray as $tag ){
//$tags .= "mytag "; // This line works when quotes are removed.
//$tags = strval($tag); //This doesn't.
fwrite($logger,$tag . "\n");
}
}
fwrite($logger,$tags . "\n");
fwrite($logger,print_r($tagarray) . "\n");
fwrite($logger,"----------\n End Debug \n\n");
fclose($logger);
精彩评论