wordpress插件启用出错如何排查?

Author: 陌小雨Date: 2020-08-31View: 120

开发WordPress插件时,如果启用时出现错误,用try...catch时抓不到错误的。WordPress有内置的错误处理方法,陌小雨分享如下:

function custom_save_error($plugin_name)
{
    $out = '===================BEGIN===================='.PHP_EOL;
    $out .= 'Time   : '.date("Y/m/d H:i:s").PHP_EOL;
    $out .= 'Plugin :'.$plugin_name.PHP_EOL;
    $out .= 'Error: '.PHP_EOL;
    $out .= ob_get_contents();
    $out .= '=====================END===================='.PHP_EOL;
    file_put_contents(ABSPATH. 'plugin_activation.log', $out, FILE_APPEND);
}
add_action('activated_plugin','custom_save_error');