Adding dd() in WordPress development

RMAG news

Hey folks! Hope you are all doing well. Today I just want to share a common topic, which is debugging in WordPress. I do like default WordPress debugging. It’s just fine. But you know what I like most is the Laravel’s dd() function. 😍

So let’s just check where and how the functions works.

if (!function_exists(‘dd’)) {
/**
* @return never
*/
function dd(…$vars)
{
if (!in_array(PHP_SAPI, [‘cli’, ‘phpdbg’], true) && !headers_sent()) {
header(‘HTTP/1.1 500 Internal Server Error’);
}

foreach ($vars as $v) {
VarDumper::dump($v);
}

exit(1);
}
}

We found the dd() function inside the Symfony component var-dumper. Oh! I love Symfony. 😍
So let’s check the function now.

[PHP_SAPI](https://www.php.net/manual/en/reserved.constants.php#constant.php-sapi) is a predefined constant in PHP. It tells us how the PHP script is being executed. So in the condition we found that !in_array(PHP_SAPI, [‘cli’, ‘phpdbg’], true) let’s breakdown this:

PHP_SAPI is being checked against an array containing ‘cli’ and ‘phpdbg’. Here ‘cli’ indicates that the script is being run from the command line interface and ‘phpdbg’ indicates that the script is being run using PHP’s interactive debugger. So this condition will be true if the script is not running in CLI or PHP Debugger mode.

[headers_sent](https://www.php.net/manual/en/function.headers-sent.php) is a function that checks if HTTP headers have already been sent to the client. So if the header is also not sent then the condition will be true.

If both conditions are true then the server will send a 500 Internal Server Error response

In the next we found this foreach function which is the main part of our dd function.

foreach ($vars as $v) {
VarDumper::dump($v);
}

So here $vars are actually items of collection. Then inside the foreach it calls the static dump() method inside the [VarDumper](https://github.com/symfony/var-dumper/blob/7.0/VarDumper.php) class.

After this foreach block there is a exit() function call to exit the operation. Pretty straight forward right!

Installation

To use this function in my WordPress plugin I needed to install this component.

From my plugin directory I ran this command:
composer require –dev symfony/var-dumper.

It was installed. But to use this in my plugin I had to add autoload inside the init action hook.

add_action(‘init’, function () {
try {
if (file_exists(__DIR__ . ‘/vendor/autoload.php’)) {
require_once __DIR__ . ‘/vendor/autoload.php’;
}
} catch (Exception $e) {
logCatchException($e);
}
}, 10, 0);

So now I can call the dd() or dump() method inside any places from my plugin. And it perfectly shows the collection or array data.

public function schedule($postId, $timestamp, $opts)
{
$timestamp = $this->convertLocalTimeToUtc($timestamp);
dd($opts);
}

Here is the data:

array:4 [▼
“expireType” => “change-status”
“newStatus” => “draft”
“category” => “0”
“categoryTaxonomy” => “category”
]

That’s all for today. Thank you all for reading it.