Skip to content
Article

WordPress Debugging – “Doing It Wrong” warnings

WordPress recently added some “helpful” error messages warning you when you’re not doing certain things correctly. Specifically, adding scripts and/or styles incorrectly using the wp_enqueue_* function.

However, even though it appears they tell you where things went wrong (via trigger_error), it actually just points you to the _doing_it_wrong function.

Fortunately, those hook-crazy developers threw us a bone in that function:
[php]
do_action( ‘doing_it_wrong_run’, $function, $message, $version );
//
[/php]

Now we can figure out the offender ourselves with a hook:
[php highlight=”13″]
/**
* Because new WP 3.3 “doing it wrong” warnings don’t really tell you where you screwed up…
* @param string $function The function that was called.
* @param string $message A message explaining what has been done incorrectly.
* @param string $version The version of WordPress where the message was added.
*/
function abt_doing_it_wrong_helper($function, $message, $version){
$before = <<<EOD
<strong>$function:</strong> v$version
EOD;
debug_whereat(3, 5, $before);
}
add_action( ‘doing_it_wrong_run’, ‘abt_doing_it_wrong_helper’, 10, 3 );
//
[/php]

And the helper function for printing debug_backtrace (use your own flavor):
[php]
/**
* Pretty-print debug_backtrace()
* @param int $limit {optional} when to stop printing – how many recursions up/down
* @param int $skip {optional} when to start printing – how many calls to skip over
* @param string $before {optional} extra html to print before the table, inside debug container
* @param string $after {optional} extra html to print before the table, inside debug container
* */
function debug_whereat($limit = false, $skip = false, $before = ”, $after = ”){
static $debug_whereat_counter; if( !$debug_whereat_counter) $debug_whereat_counter = 0;
$uid = $debug_whereat_counter++;
?>
<div class=”debug trace”>
<?php echo $before; ?>
<table>
<thead><tr>
<th id=”th-index-<?=$uid?>”><i>nth</i></th>
<th id=”th-line-<?=$uid?>”>Line</th>
<th id=”th-file-<?=$uid?>”>File</th>
<th id=”th-method-<?=$uid?>”>Method</th>
</tr></thead>
<tbody>
<?php

$backtrace = debug_backtrace();
if( $skip ) $backtrace = array_slice($backtrace, $skip);

foreach($backtrace as $index => $trace){
//force quit
if($limit !== false && $index == $limit){
?>
<tr><td colspan=”4″><em>—– FORCE STOP RECURSION —–</em></td></tr>
<?php
break;
}

?>
<tr class=”trace-item”>
<th headers=”th-index-<?=$uid?>”><?=$index?></th>
<td headers=”th-line-<?=$uid?>” class=”line”><?=$trace[‘line’]?></td>
<td headers=”th-file-<?=$uid?>” class=”file”><?=$trace[‘file’]?></td>
<td headers=”th-method-<?=$uid?>” class=”method”>
<code><?=$trace[‘function’]?></code>
<?php
if(!empty($trace[‘args’])){
echo ‘<br />’;
while(!empty($trace[‘args’])){
?> {<i><?php print_r(array_shift($trace[‘args’]) ); ?></i>} <?php
}// while !empty $trace[‘args’]
}
?>
</td>
</tr>
<?php
}
?>
</tbody></table><?php echo $after; ?></div>
<?php

}// function debug_whereat
[/php]

The Atlantic BT Manifesto

The Ultimate Guide To Planning A Complex Web Project