Wordpress php print to console

You can write a utility function like this:

function prefix_console_log_message( $message ) {

    $message = htmlspecialchars( stripslashes( $message ) );
    //Replacing Quotes, so that it does not mess up the script
    $message = str_replace( '"', "-", $message );
    $message = str_replace( "'", "-", $message );

    return "";
}

The you may call the function like this:

echo prefix_console_log_message( "Error Message: This is really a 'unique' problem!" );

and this will output to console like this:

Error Message: This is really a -unique- problem!

Notice the quotes replaced with "-". It is done so that message does not mess up your script as pointed by @Josef-Engelfrost

You may also go one step further and do something like this:

function prefix_console_log_message( $message, $type = "log" ) {

    $message_types = array( 'log', 'error', 'warn', 'info' );
    $type          = ( in_array( strtolower( $type ), $message_types ) ) ? strtolower( $type ) : $message_types[0];
    $message       = htmlspecialchars( stripslashes( $message ) );
    //Replacing Quotes, so that it does not mess up the script
    $message = str_replace( '"', "-", $message );
    $message = str_replace( "'", "-", $message );

    return "";
}

and call the function like this:

echo prefix_console_log_message( "Error Message: This is really a 'unique' problem!" , 'error');

It will output error in console.

Wordpress php print to console

Debugging PHP can be a pain in WordPress. Add this handy code snippet to your theme’s functions.php file. The function will log PHP data to your browser’s console.

/**
 * @param ...$data
 *
 * @return void
 */
function my_console_log(...$data) {
    $json = json_encode($data);
    add_action('shutdown', function() use ($json) {
       echo "";
    });
}

// Example usage
my_console_log(['foo' => 'bar'], null, 1, 'hi');

Kevin Dees is a speaker, entrepreneur, blogger, and developer. He’s best known for his work in WordPress and podcasting for the retired SitePoint show. Kevin has developed sites for the brands Verizon, Denny’s, RIDGID, Michelin, and others. Currently, he is working on TypeRocket which powers over 10,000 professional websites and products internationally.

See all posts by Kevin Dees

  • Details
  • Reviews
  • Support
  • Development

WP Console brings the famous PsySH on your browser. PsySH is a runtime developer console, interactive debugger and REPL for PHP.

Write your code in code editor, press Cmd-Enter(mac) or Ctrl-Enter(win/linux) and get your output in your browser.

You can also use PsySH with wp-cli with the command wp shell. wp-cli has built-in support for psysh already. You need to just activate WP Console to use this.

Features

  • Powerful code editor powered by Ace Editor.
  • PHP core and WordPress functions live autocompletion with placeholders.
  • _dump as the alternative to var_dump which uses Symfony VarDumper.
  • Get debug.log contents and clear them right from your browser.
  • Advanced shell powered by psySH for wp shell.
  • Custom code snippet. Supports VS Code supported code snippets. For example you can use this WooCommerce snippets.

Please note that, currently PsySH commands like ls, doc, show etc and Magic variables like $_, $__class etc are not supported in browser console.

👉 WP Console uses Gutenberg packages and components for its UI/UX. Checkout the GitHub repo here ediamin/wp-console.

Security Concern

WP Console explicitly checks for manage_options permission to display the UI and perform other actions. Yet, this plugin should not be used in production server.

Super useful for testing functionality and inspecting WordPress data.

Very useful tool, thank's a lot! Many things easier to do with code, with no plugins and that is perfect tool for this. Very good!

This is an amazing plugin if you need to test a snippet on a live site or debug the state of the live site. Just remember to uninstall and remove it when you are done.

php artisan tink... Wait, it's a WordPress project. :/ WP Console aka wp shell to the rescue! Love it!

Really Awesome Plugin! Works as Expected!

Read all 10 reviews

“WP Console – WordPress PHP Console powered by PsySH” is open source software. The following people have contributed to this plugin.

Contributors

  • Edi Amin

2.3.1 – July 31, 2022
* Update script version of ace.

2.3.0 – July 27, 2022
* Add code execution time in console output panel.
* Use ace.js with custom namespace to avoid conflict with other ace.js source.
* Fix snippet manager module style.
* Use verticle split as default in console panel.
* Fix error handling for PHP v5.6.
* Fix some UI issues in different WordPress versions.
* Add wp-env and e2e testing with Jest using wp-scripts for developing the plugin.

2.2.0 – November 22, 2020
* Add custom code snippet support. You can use VS Code supported PHP code snippets now.
* Add Copy Output button.
* Enqueue scripts only for manage_options capability owners.
* Improve handling uncaught fatal errors.
* Add Twenty Twenty theme compatibility.
* Set default values for user Console settings in REST API.
* Fix horizontal output scolling issue for vertically split console.
* Fix close button get disappear in WooCommerce admin pages.
* Fix error line no in console editor.

2.1.0 – April 14, 2020
* Lazy load React components to improve performance.
* Use a single store source for all components.
* Reset console responses after close app window.
* Fix navigation button icon css for WP v5.4.
* Resize editor screen after toggle split mode.

2.0.0 – December 11, 2019
* Revamp UI/UX.
* Add Ace editor as code editor plugin. Remove CodeMirror.
* Live autocompletion with placeholders.
* Option to vertically split editor and output window.
* Add ability to clear debug.log.
* Restrict plugin UI and REST APIs for users who have manage_options capability.

1.5.0 – November 07, 2019
* Tweak – Change dump function name to _dump to resolve conflict with wp-erp.

1.4.0 – November 02, 2019
* New – Save code editor history in localStorage.
* Tweak – Use wp-scripts for assets build process.

1.3.0 – October 26, 2019
* New – Fetch debug.log contents.
* Tweak – Support PHP version 5.6.

1.2.0 – July 27, 2019
* New – Add autocompletion data(WP functions, PHP booleans, constants, functions, keywords).
* New – Add codemirror closebrackets, matchbrackets addon scripts.
* New – Show error stacktrace.

1.1.0 – June 25, 2019
* New – Use output buffer handler.
* New – Add build process.
* Tweak – Remove unnecessary code.

1.0.0 – June 21, 2019
Initial release.

How can I print to console in PHP?

The echo command is used in PHP to print any value to the HTML document. Use