Shoora
4/28/2019 - 12:16 PM

[WordPress] Debug helper. Dumps code for a given logged-in user only.

[WordPress] Debug helper. Dumps code for a given logged-in user only.

<?php
/**
 * Plugin Name: __Degnome
 * Description: Dump code for a given logged-in user only.
 * Version:     2016-02
 * Author:      Caspar Hübinger
 * Author URI:  https://profiles.wordpress.org/glueckpress
 * License:     GNU General Public License v3 or later
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 */

defined( 'ABSPATH' ) or die( 'You know better.' );

/**
 * Define expression to be dumped for specific user only.
 * @uses                            degnome__user_debug_info()
 * @return string                   Formatted debug info
 */
function degnome() {

	// SET THIS TO YOUR USER LOGIN!
	$username = 'PUT_YOUR_WP_USER_NAME_HERE';

	// Example 1: dump active plugins.
	$dump = get_option('active_plugins');

	// Example 2: dump sitewide active plugins.
	// $dump = get_site_option('active_sitewide_plugins');

	degnome__user_debug_info( $username, $dump, 'raw' );
}
add_action( 'admin_init', 'degnome', PHP_INT_MAX );

/**
 * Helper: dumps debug info for specific username.
 * @uses                            degnome__debug_info()
 * @param  string $debug_user       Username to display debug info for.
 * @param  mixed $debug_expression  Expression to be displayed.
 * @param  string $debug_method     raw|empty string: var_dump or var_export
 * @return string                   Formatted debug info
 */
function degnome__user_debug_info( $debug_user, $debug_expression, $debug_method = '' ) {

	if ( ! is_user_logged_in() )
		return;

	if ( 'PUT_YOUR_WP_USER_NAME_HERE' === $debug_user )
		return;

	$current_user = wp_get_current_user();
	$current_user_name = $current_user->user_login;

	if ( $current_user_name !== $debug_user )
		return;

	if ( function_exists( 'degnome__debug_info' ) )
		degnome__debug_info( $debug_expression, $debug_method );
}

 /**
  * Helper: dumps debug info.
  * @uses                            wp_die()
  * @param  mixed $debug_expression  Expression to be displayed
  * @param  string $debug_method     raw: var_dump|empty: print_r
  * @return string                   Formatted debug info
  */
function degnome__debug_info( $debug_expression = '', $debug_method = '' ) {

	$dump = print_r( $debug_expression, true );

	if ( 'raw' === $debug_method ) {
		ob_start();
		var_dump( $debug_expression );
		$dump = ob_get_clean();
	}

	$formatted = "<pre>$dump</pre>";

	wp_die( $formatted, 'Degnoming' );
}