Neo
Neo

Reputation: 13891

Help debugging php code

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 quotes 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?

Upvotes: 0

Views: 265

Answers (7)

Nathan
Nathan

Reputation: 11

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.

Upvotes: 1

EAMann
EAMann

Reputation: 4146

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 );
    }
}

Upvotes: 1

Phoenix
Phoenix

Reputation: 4536

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);

Upvotes: 0

Anush Prem
Anush Prem

Reputation: 1481

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

Upvotes: 0

piddl0r
piddl0r

Reputation: 2449

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.

Upvotes: 1

Poelinca Dorin
Poelinca Dorin

Reputation: 9703

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 .

Upvotes: 1

sshet
sshet

Reputation: 1160

for debugging use var_dump or print_r

Upvotes: 0

Related Questions