How to write own DD() function same as laravel?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Recreating Laravel's DD() Function for WordPress - A Comprehensive Guide Body: As developers, we often need efficient ways to debug and analyze the contents of variables in our code. While working with frameworks like Laravel, you may have utilized the DD() function which provides a convenient way to dump arrays and objects. However, if you are currently using WordPress and looking for an alternative method, implementing your own version of the DD() function can be quite straightforward. Firstly, we need to create a new file or include it in our project's functions.php file (if using a child theme). The following code provides the basis for your custom dd() function:
function dd($data) {
   echo "<pre>";
   call_user_func_array('var_dump', func_get_args());
   echo "</pre>";
   die();
}
This function accepts an argument in the form of any data type (including arrays or objects) and outputs it in a pre-formatted manner, similar to Laravel's DD() function. The main difference is that this version is not styled by default, but you can add styling to improve readability if needed. If you are comfortable with CSS, you can add the following code snippet within your functions.php file:
add_action('wp_head', function() {
   $style = ".dd-wrap {border: 1px solid black; background-color: #eee; padding: 20px;}";
   wp_enqueue_script( 'my_custom_script', get_stylesheet_directory_uri() . '/js/custom.js' );
   wp_add_inline_script('my_custom_script', $style, 'before');
});
The above code adds a custom style to your dd() output and enqueues the script (you can modify paths as needed) with inline CSS. The HTML structure within the style should look like this:
<div class="dd-wrap"></div>
To further improve readability, you could also use jQuery to replace the pre-formatted output with a custom styling. Here's an example of how this can be done inside your custom.js file:
jQuery(document).ready(function($) {
   $('pre').each(function() {
      var html = $(this).html();
      if ($('.dd-wrap').length == 0) {
         $('
').insertAfter($(this)).append(html); } else { $('.dd-wrap').prepend('
<pre>' + html + '</pre>
'); } }); });
This code replaces any existing pre tags with a custom structure styled using the '.dd-wrap' class. For better styling, you can add CSS rules for this new HTML structure:
.dd-wrap {
   border: 1px solid black;
   background-color: #eee;
   padding: 20px;
}
By implementing these simple adjustments, you can create a customized version of the Laravel DD() function for your WordPress project. Although it is not as feature-rich as the original Laravel implementation, this solution provides an accessible and efficient way to debug and analyze variables in your code. Remember that using the above code snippets will require including the functions.php file or loading the custom.js script within your theme files. It's essential to ensure that they are only loaded when needed to avoid unnecessary overhead on your site. For a more comprehensive guide, you can refer to resources such as Laravel Company's official website which offers in-depth documentation and tutorials on how to efficiently use the DD() function within Laravel projects.