When working on a plugin (or theme!) how many times do you print_r()
or var_dump()
to see what data is stored in some variable, array, or object?
Here’s the functions I use when creating plugins. I just drop the file into my C:/
drive and then do a include_once('C:/debug.php');
in my wp-config.php
file (on localhost). This gives gives me access to the functions everywhere.
These functions should not be placed on a production website!
debug()
The debug()
function takes 1-4 arguments. It exposes the data in variables, arrays, objects. It tells you the data type and the value. See the comments with the function. As a basic example, doing this:
debug($my_array, 'xyz');
…might produce something like this…
$xyz [array]
Array
(
[type] => all
[color] => multi
[size] => small
)
line()
The line()
function takes the same arguments, except that it only works with basic variables. This function is best suited for use in loops where you want to expose the data in a series of k/v pairs. See the comments with the function. As a basic example, doing this:
$my_array = [
'type' => 'all',
'color' => 'multi',
'size' => 'small'
];
foreach ($my_array as $k=>$v) {
line($v);
}
foreach ($my_array as $k=>$v) {
line($v, $k);
}
…might produce something like this…
> all [string]
> multi [string]
> small [string]
> $type = all [string]
> $color = multi [string]
> $size = small [string]
And that’s it. Once included, you can use these functions anywhere you might find yourself in a day of ClassicPressing. PS. If I’ve shared my debug()
and line()
functions with you previously, these are updated versions!