Using Laravel d() and dd() helpers in WordPress
Tags: wordpress, webdev, php, tutorial
Published: 2019-10-13 18:02:13

---
title: Using Laravel d() and dd() helpers in WordPress
published: true
description: A tutorial on how to implement Laravel's d() and dd() debugging helper functions in WordPress.
canonical_url: https://since1979.dev/using-laravel-d-and-dd-helpers-in-wordpress/
cover_image: https://since1979.dev/wp-content/uploads/2019/10/wordpress-using-laravel-d-and-dd-helpers-1000x450.jpg
tags: wordpress, webdev, php, tutorial
---

[Originally posted on my website on October 13th 2019](https://since1979.dev/using-laravel-d-and-dd-helpers-in-wordpress/)

### Introduction

Besides being a "WordPress enthusiast" I'm also a fan of the Laravel framework. And sometimes Laravel has some nice features that can be transported to WordPress. I already wrote an article [about using Laravel-Mix with WordPress](https://since1979.dev/wordpress-laravel-mix-sass-and-es6-modules-in-theme-development/), and in this article i want to talk about the *d()* and *dd()* debugging functions.

If you have ever worked with [the Laravel framework](https://laravel.com/), or maybe you watched a tutorial, you probably would have noticed the use of the *d()* *(Dump)* and [*dd()*](https://laravel.com/docs/master/helpers#method-dd) *(Dump and Die)* debugging helper functions.

These two functions are an amazing alternative to Php's own *[print_r()](https://www.php.net/manual/en/function.print-r.php)* and *[var_dump()](https://www.php.net/manual/en/function.var-dump.php)* functions. Not only are they easier to type, they also pretty print their output like shown below. And trust me, once you start using them you never want to develop Php without them ever again.

#### Using the Laravel d() and *dd()* functions.

I've seen quite a lot of developers use a code snippet like below to visualize the contents of variables. Maybe this looks familiar!?

```php
echo '<pre>';
print_r( $somevar );
echo '</pre>';
die();
```

Now using the *d()* and *dd()* helpers is much more straightforward and elegant. Check out this little example.

```php
$array = array( 'one' => 'test one', 'two' => 'test two');
dd( $array );
```

This example would generate an output like shown in the image below.

![](https://since1979.dev/wp-content/uploads/2019/10/laravel-dd-usage-example.png)

Nice huh?

I hear you thinking "That's all nice and fun, but these functions don't exist in WordPress"!? That's true! But that doesn't mean we can't add these our self.

### Symfony Var-Dumper

Under the hood these two functions use the [*Var-Dumper*](https://github.com/symfony/var-dumper) package from the [Symfony Php framework](https://symfony.com/). So to create our own *d()* and *dd()* helpers we have to use [Composer](https://getcomposer.org/) to require a copy of this package.

If you don't have composer installed yet you can check out [the documentation here](https://getcomposer.org/doc/00-intro.md).

To install the package run the following command in the root folder of your theme or plugin.

```console
$ composer require symfony/var-dumper --dev
```

This will download a copy of the package and place it into the vendor/symfony directory. And add it to your *composer.json* file as a development dependency.

### Autoload.php

To use packages installed by composer in your code you have to include a file called *autoload.php* from the *vendor* directory. To do so you can put the code snippet below into you *functions.php*. Or if you are working on a plugin you can require the *autoload.php* file somewhere in your main plugin file.

```php
require_once __DIR__ . '/vendor/autoload.php';
```

### Adding d() and dd() helpers to WordPress

Now that we have autoloading and the *Var-Dumper* package in place we can create our own *d()* and *dd()* functions. Copy the code below into your *[functions.php](https://codex.wordpress.org/Functions_File_Explained)* file.

```php
/*
|--------------------------------------------------------------------------
| Helper functions
|--------------------------------------------------------------------------
*/

/**
 * Dump variables.
 */
if ( ! function_exists('d') ) {
    
    function d() {
        call_user_func_array( 'dump' , func_get_args() );
    }

}

/**
 * Dump variables and die.
 */
if ( ! function_exists('dd') ) {

    function dd() {
        call_user_func_array( 'dump' , func_get_args() );
        die();
    }

}
```

Both functions a pretty simple. All they do is use *[call_user_function_array()](https://www.php.net/manual/en/function.call-user-func-array.php)* to call the *dump()* function from the *var-dumper* package and pass along any arguments using the *[func_get_args()](https://www.php.net/manual/en/function.func-get-args.php)* function. The *dd()* function also stops the code execution by calling the php *[die()](https://www.php.net/manual/en/function.die.php)* function.

You can now use the helper functions anywhere like shown below.

```php
$string = "I'm a string";

$array = array( 'one' => 'test one', 'two' => 'test two');

d( $string );

dd( $array , $string );
```

Note that you can pass any number, or types, of variables. Our new functions will visualize them in sequence like shown below.

![](https://since1979.dev/wp-content/uploads/2019/10/laravel-dd-output.png)

### Conclusion

I can't fully explain how great it is to have these helpers. So i can only recommend you try them for yourself. They can really take away some of the debugging pain, and I bet you will love them two. 😉

**Follow me on twitter** [**@Vanaf1979**](https://twitter.com/Vanaf1979) **or on Dev.to** [**@Vanaf1979**](https://dev.to/vanaf1979) **to be notified about new articles, and other WordPress development related resources.**

**Thanks for reading.**