Skip to content

Instantly share code, notes, and snippets.

@AashikP
Last active April 28, 2021 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AashikP/408583ddd7ad504b613915104507738d to your computer and use it in GitHub Desktop.
Save AashikP/408583ddd7ad504b613915104507738d to your computer and use it in GitHub Desktop.
var_dump as dashboard notice.
<?php
/**
* Function to var_dump contents as WordPress Dashboard notification.
*
* Instructions:
* Optional argument $file can be supplied as __FILE__ $line supplied as __LINE__ if you also want to
* print out the file and line number (for whatever reason) where this function is called.
*
* @param variable $dump_this Will dump the variable as an admin notice.
* @param string $file Original file link.
* @param string $line number where the functino is executed.
*/
function dd( $dump_this, $file = 'NA', $line = 'NA' ) {
add_action(
'admin_notices',
function () use ( $dump_this, $file, $line ) {
echo '<div class="notice is-dismissible">';
if ( 'NA' !== $file ) {
echo '<span style="color:#be5046;font-weight:600">' . esc_html( $file ) . '</span>';
}
if ( 'NA' !== $line ) {
echo '<span style="color:#98c379;font-weight:600"> : ' . esc_html( $line ) . '</span>';
}
var_dump( $dump_this ); // phpcs:ignore
echo '</div>';
return;
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment