We've Always Got Something to Say.

Read our thoughts on what's happening in the industry.

January 16 , 2012

WordPress Debugging – “Doing It Wrong” warnings

by

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:

do_action( 'doing_it_wrong_run', $function, $message, $version );
//

Now we can figure out the offender ourselves with a hook:

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

And the helper function for printing debug_backtrace (use your own flavor):

/**
 * 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

About the Author

Jeremy Schwartz is a software developer with a broad range of programming experience from research, consulting, and academic settings. His favorite thing to do is refactor inefficient code into well-documented, elegant solutions using content-management systems and custom code. Or read books. Lots of books.

Leave a Reply

 

In a Nutshell

Since 1998, Atlantic BT has been a full service web development company that offers the tools, resources and services to get your business moving. We focus on combining new ideas, specific requirements, and years of experience into high-quality, results-oriented web solutions for small to medium sized businesses. If you want the best website possible that generates real results, let's get started.

Atlantic Business Technologies, Inc.
4509 Creedmoor Road, 3rd Floor
Raleigh, North Carolina 27612
  • Pinnacle Business Award Winner
  • Triangle Business Journal's Top 40 Under 40
  • Yahoo Search Marketing Ambassador
  • Google Adwords Qualified Company