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, and in this article i want to talk about the d() and dd() debugging functions.

If you have ever worked with the Laravel framework, or maybe you watched a tutorial, you probably would have noticed the use of the d() (Dump) and dd() (Dump and Die) debugging helper functions.

These two functions are an amazing alternative to Php’s own print_r() and var_dump() 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!?

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

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

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 package from the Symfony Php framework. So to create our own d() and dd() helpers we have to use Composer to require a copy of this package.

If you don’t have composer installed yet you can check out the documentation here.

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

$ 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.

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 file.

Both functions a pretty simple. All they do is use call_user_function_array() to call the dump() function from the var-dumper package and pass along any arguments using the func_get_args() function. The dd() function also stops the code execution by calling the php die() function.

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

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

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. 😉

If you want to leave a comment, please do so under the copy of this article on Dev.to so i can get back to you.

Follow me on twitter @Vanaf1979 or on Dev.to @Vanaf1979 to be notified about new articles, and other WordPress development related resources.

Thanks for reading.

Tags: ,