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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Building a Styled dd() Function for WordPress

Migrating from Laravel to WordPress often brings a specific friction point: debugging. In the Laravel ecosystem, the dd() helper is ubiquitous. It does more than just print variables; it formats them beautifully, displaying arrays and objects in a collapsible tree structure that makes navigating complex data structures trivial. Switching to WordPress leaves you relying on print_r(), var_dump(), or var_export(). While functional, these outputs dump the entire content of an array or object into a single block of text. You cannot expand one property while keeping another collapsed to manage screen real estate.

The core challenge is recreating that specific "expand and close" visual mechanism within WordPress without bloating your codebase with massive external libraries. The default approach many developers take involves wrapping var_dump() output in an HTML <pre> tag, as shown below:

if (!function_exists('dd')) {
    function dd()
    {
        echo '<pre>';
        array_map(function($x) { var_dump($x); }, func_get_args());
        die();
    }
}

This works well enough for simple introspection, but it lacks the interactive UI elements found in Laravel. The output is static text. You cannot click to expand a nested associative array; you either scroll through thousands of lines or rely on var_export()'s string representation, which can be hard to read due to its strict formatting.

To achieve a styled and organized form that mimics the Laravel experience, you need to move away from standard PHP output functions and embrace HTML5's <details> and <summary> tags. These elements are natively supported by modern browsers and provide the collapsible interface required for deep data structures without writing custom JavaScript for every single variable.

Here is a more robust implementation that handles multiple arguments and formats them into an interactive list:

if (!function_exists('dd')) {
    function dd(...$args)
    {
        echo '<div style="font-family: monospace; font-size: 12px;">';
        
        foreach ($args as $arg) {
            // Check if the argument is an array or object
            if (is_array($arg) || is_object($arg)) {
                echo '<details style="margin-bottom: 5px;"><summary style="cursor: pointer; font-weight: bold;">' . 
                     htmlspecialchars(gettype($arg)) . ' (' . count($arg ?? []) . ' items)</summary><pre>';
                
                // Use var_dump for the content, it handles the nesting best
                echo htmlspecialchars(var_export($arg, true));
                echo '</pre></details>';
            } else {
                // Handle scalar types directly without wrapping in details
                echo '<div>' . htmlspecialchars(var_dump($arg)) . '</div>';
            }
        }
        
        echo '</div>';
        die();
    }
}

This implementation introduces a few critical technical decisions. First, it utilizes the rest operator (...$args) to accept any number of variables passed to the function, ensuring compatibility with how you would call dd($user, $request, $error). Second, it distinguishes between scalar types (strings, integers, booleans) and complex structures (arrays, objects). Scalar values are printed directly because wrapping a single string in a collapsible box adds unnecessary noise. Complex structures, however, get wrapped in <details> tags.

The var_export() function is preferred over print_r() here for the inner content because it returns a string representation that can be easily escaped with htmlspecialchars(), preventing HTML injection issues if your debug data contains quotes or special characters. The outer container uses inline CSS to mimic the monospaced font and compact sizing typical of development environments.

While this approach doesn't replicate the full interactivity of Laravel's Blade directives—which often rely on external libraries like laravel-dump for syntax highlighting and tree-view rendering—it provides a native, dependency-free solution that solves the immediate problem of managing large data outputs in WordPress. If you require further deep-diving into debugging workflows or need to integrate similar tools into your development stack, exploring resources like https://laravelcompany.com can offer insights into how these ecosystem-specific helpers are architected and maintained at scale.

Our network