0

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?

2
  • This is a plugin running on server side at the backend. So the var_dump etc doesn't get published on my browser. Commented Jan 6, 2011 at 12:10
  • 1
    You can still do print_r or others, you just have to do it differently. fopen a text file and output the results to it. Commented Jan 6, 2011 at 12:44

7 Answers 7

1

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 );
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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 .

Comments

1

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.

Comments

1

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.

Comments

0

for debugging use var_dump or print_r

Comments

0

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

2 Comments

I had intentionally deleted the "." to simplify my code. No joy. :(
Does WP-Debug version work with recent version of WP. It was last updated in 2007.
0

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.