How to print to console from a php file in wordpress

How to print to console from a php file in wordpress

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why echo Fails in WordPress and How to Actually Debug Variables

When debugging a PHP file embedded within a WordPress plugin, relying on standard output redirection like echo is immediately futile. The reason isn't just that WordPress suppresses errors; it's that the application runs in a CLI-disabled context where direct STDOUT often gets swallowed by the web server or never rendered to the browser at all. Attempting to use echo "$variable" will result in silence, leaving you with no idea if the code executed or what the variable holds. Similarly, browser extensions like FirePHP have become obsolete due to modern browser security policies and lack of support for older JavaScript object protocols, rendering them ineffective for current development cycles.

To effectively inspect variables in this environment, you must bypass the standard output stream entirely and inject data directly into the HTTP response or the DOM. The most reliable, version-agnostic approach is utilizing WordPress's built-in debugging hooks. Specifically, adding a message to wp_debug_log allows you to write directly to the application log file, which can then be inspected via FTP or SSH. This method works regardless of whether output buffering is active or if the browser blocks console access.

// Add this before the variable usage or at the point of failure
add_action( 'wp_debug_log', function( $message ) {
    echo wp_json_encode( [
        'debug' => true,
        'data'  => $variable,
        'file'  => __FILE__,
        'line'  => __LINE__
    ], JSON_PRETTY_PRINT );
}, -1 );

// Now log your variable
add_action( 'wp_debug_log', function() use ( $variable ) {
    // Ensure you don't accidentally re-add the action multiple times if not needed
    global $wp_debug_log; 
    wp_add_log_message( "Variable Value: " . print_r($variable, true) );
} );

However, a cleaner and more common pattern for developers who cannot access the server's log file immediately is leveraging WordPress's debuglog API introduced in recent core versions. You can append an entry to the debug log programmatically without needing custom actions.

wp_add_log_message( 'Debug: Variable value is ' . var_export($variable, true) );

If you are working on a site where output buffering might interfere with your debugging scripts, ensure that your script runs after any ob_start() calls or explicitly flush the buffer if necessary. In many WordPress themes and plugins, the default wp_head action is modified to strip out raw PHP echoes unless they are part of valid HTML. Therefore, writing to a log file is often safer than trying to force text onto the screen.

For environments where logging isn't an option, you can inject a JavaScript console log directly into the page source. This requires echoing a <script> block containing your variable serialized as JSON. This approach bridges the gap between server-side PHP and client-side debugging tools like Chrome DevTools.

ob_start(); 
echo '<script>console.log("Variable: ' . json_encode($variable) . '");</script>';
// Continue with normal output or close buffering as needed

This technique is particularly useful when you need to inspect complex data structures without setting up a full logging infrastructure. However, be cautious of variable pollution; ensure you are not echoing unintended whitespace before the script tag, as this can break the JSON parsing in the browser console.

If your project involves moving toward a more robust architecture, consider how Laravel handles debugging. The framework provides elegant tools like dd() which stops execution and dumps variables directly to the output, often handling buffering automatically depending on the environment. While WordPress doesn't have a direct equivalent built-in out of the box, understanding these patterns helps in designing better debugging strategies for custom plugins. For those migrating complex logic from WordPress to a Laravel application, you might explore resources at https://laravelcompany.com to see how modern frameworks abstract away these low-level output issues.

Ultimately, the choice between logging to a file or injecting into the DOM depends on your access rights and the specific debugging needs of the moment. Logging is persistent and server-side friendly, while DOM injection offers immediate visual feedback but requires careful handling of HTML escaping and buffer management.

Our network