$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>debug - Easy code troubleshooting in wordpress|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

debug - Easy code troubleshooting in wordpress

matteradmin8PV0评论

Is there an easy and standard way to debug our code in wordpress plugins? I want to print some variables in a function that runs when a certain things happen. Is there a wordpress way to save this in a file or at least see it?

Is there an easy and standard way to debug our code in wordpress plugins? I want to print some variables in a function that runs when a certain things happen. Is there a wordpress way to save this in a file or at least see it?

Share Improve this question asked Mar 4, 2019 at 22:37 AmiritionAmirition 3555 silver badges20 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 7

The best way to troubleshoot/debug your PHP code for WordPress is to use the XDEBUG debugger for PHP coupled with an IDE/editor that can leverage XDEBUG.

By far my favorite IDE for PHP/XDEBUG is PhpStorm (best money I have ever spent), but you can use for free on the bleeding edge with their EAP (Early Access Program). You can also use the free VSCode with XDEBUG.

Here is a great video showing what it looks like to use XDEBUG & PhpStorm. You can start at the 18:00 point because everything before that is talking and slides.

You will also need a local development setup that supports XDEBUG. Several years ago I was frustrated with how difficult it was to get XDEBUG configured correctly so I had my team develop WPLib Box which pre-configures XDEBUG and thus makes using XDEBUG trivally easy. You can download WPLib Box from GitHub, use for free, and get help via our Slack.

I like to say using PhpStorm/XDEBUG/WPlib Box gives me WordPress superpowers. How else do you think I answered all the questions here at WPSE that I have? :-)

That said, there are also other local development solutions such as VVV, Trellis, Chassis, Desktop Server, MAMP, DevilBox, LaraDock, Kalabox, Kusanagi and others you might try. But I am not sure how difficult it is to set up XDEBUG to work with any of these others anymore but I know firsthand XDEBUG works without any fuss when using WPLib Box.

Hope this helps.

Just start with the following definitions in your config.php file:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('SCRIPT_DEBUG', true);
define('SAVEQUERIES', true);

The first one sets your system into debug mode, the second one writes every notice into a log file saved into your wp-content directory. Script debug enables WordPress to use the uncompressed CSS and JS files (Maybe also interesting for writing Plugins). And Savequeries writes HTML-Comments at the bottom of the rendered HTML Output and will be used by Plugins I suggest you next.

See the WordPress Plugin Directory for Debug Bar and extensions. There are a lot and there's a great chance that some of them are providing exactly what you need.

If nothing helps you can write everytime

print_r( $var );
die();

and look into the HTML sourcecode.

Unfortunately there isn't really a standard developer logging API in WordPress. If you want to output your own data to a debug file (to prevent "headers already sent" errors) then you can simply use the standard PHP error_log function. Something simple like this will record the variable $data to the file debug.log in your plugins directory:

$debugline = '['.date('y m d H:i:s', time()).'] '.print_r($data,true);
$debugfilepath = dirname(__FILE__).'/debug.log';
error_log($debugline, 3, $debugfilepath); 

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far