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

Insights

Atlantic BT's Insights

We’re sharing the latest concepts in tech, design, and software development. Learn more about our findings.

Questions & Answers

What is the best web development framework?
Many people commonly ask “what is a framework in web development?” Web development frameworks can easily be confused with web development tools, languages, or parts of the web development stack (like .NET, PHP, JavaScript, or Ruby).
Learn More about What is the best web development framework?
What is the best programming language for web development?
If there was one “best” programming language, then everything else would be obsolete. The reality is that there are so many different programming languages because there is no “best” language for any situation.
Learn More about What is the best programming language for web development?
How much does web development cost?
Web development can vary from a few hundred to millions of dollars depending on what is needed. You may simply need some changes to something that already exists, or you'd like to build a large or complex application.
Learn More about How much does web development cost?
What is PHP web development?
PHP is a back end language primarily used for custom applications, content management systems (such as Wordpress), eCommerce engines (such as Magento), or even massive sites like Facebook.
Learn More about What is PHP web development?
What is the best way to become a web developer?
We get lots of questions from university students working on projects -- How do I get into web development? How long does it take to learn? How much do web developers make?
Learn More about What is the best way to become a web developer?
What is front end vs. back end development?
As web development evolved, it separated into logical specialization: front end web development and back end development. While back end development involves the server-side development, front end is the final rendering.
Learn More about What is front end vs. back end development?
What is full stack web development?
Full stack web development as a term evolved due to the separation of roles between front end and back end developers. A “full stack” developer is a developer that can work in both front end and back end technologies.
Learn More about What is full stack web development?