Send PHP Values to the Browser Console
Adds a wpide_console() helper that prints any PHP variable to the browser's developer console for administrators, so you can inspect data without breaking the page layout.
Opens WPIDE on
Code
if ( ! function_exists( 'wpide_console' ) ) {
function wpide_console( $value, $label = '' ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$GLOBALS['wpide_console_queue'][] = array( (string) $label, $value );
}
}
$wpide_console_flush = function () {
if ( empty( $GLOBALS['wpide_console_queue'] ) ) {
return;
}
echo '<script>';
foreach ( $GLOBALS['wpide_console_queue'] as $entry ) {
list( $label, $value ) = $entry;
printf( 'console.log(%s, %s);', wp_json_encode( $label ? "PHP: $label" : 'PHP:' ), wp_json_encode( $value, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_HEX_TAG | JSON_HEX_AMP ) );
}
echo '</script>';
$GLOBALS['wpide_console_queue'] = array();
};
add_action( 'wp_footer', $wpide_console_flush, PHP_INT_MAX );
add_action( 'admin_footer', $wpide_console_flush, PHP_INT_MAX );
Notes
Example wpide_console( $post, 'Current post' ); values are queued and printed in the footer of front-end and admin pages.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.