DEV Community

Cover image for Using Laravel d() and dd() helpers in WordPress
Stephan Nijman
Stephan Nijman

Posted on • Updated on • Originally published at since1979.dev

Using Laravel d() and dd() helpers in WordPress

Originally posted on my website on October 13th 2019

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!?

echo '<pre>';
print_r( $somevar );
echo '</pre>';
die();
Enter fullscreen mode Exit fullscreen mode

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

$array = array( 'one' => 'test one', 'two' => 'test two');
dd( $array );
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

require_once __DIR__ . '/vendor/autoload.php';
Enter fullscreen mode Exit fullscreen mode

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.

/*
|--------------------------------------------------------------------------
| 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();
    }

}
Enter fullscreen mode Exit fullscreen mode

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.

$string = "I'm a string";

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

d( $string );

dd( $array , $string );
Enter fullscreen mode Exit fullscreen mode

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

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.

Top comments (2)

Collapse
 
edemir206 profile image
Edemir Santos de Paula

Just dropping here to say thank you, as I use Bedrock it was as simple as doing a "composer require symfony/var-dumper --dev" and dd() started working like a charm.

Thanks.

Collapse
 
vanaf1979 profile image
Stephan Nijman

Thank you Exemir. I personaly haven’t used bedrock. But i’m actually kinda suprises that it doesn’t have the var-dumper package by default! Thanks again. I’m happy my article helped you out a bit! 😊