如何在WordPress中调试save_post操作?

sgb*_*sgb 18 php debugging wordpress post

我有一些自定义的后期元生成并准备添加到帖子的元.我知道如何执行此操作但是save_post在POST数据发送后会导致重定向.这意味着我被重定向到仪表板并且无法访问我的POST数据 - 因此我无法轻松调试.

目前使用的东西如下:

add_action('save_post', 'something_process');

function something_process() {
   if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
       return;
   print_r($_POST);
}
Run Code Online (Sandbox Code Playgroud)

有没有办法轻松调试这个?

dan*_*ker 17

对我来说最好的方法是使用函数将值记录到wp-content/debug.log,取自http://fuelyourcoding.com/simple-debugging-with-wordpress:

if(!function_exists('log_it')){
 function log_it( $message ) {
   if( WP_DEBUG === true ){
     if( is_array( $message ) || is_object( $message ) ){
       error_log( print_r( $message, true ) );
     } else {
       error_log( $message );
     }
   }
 }
}
Run Code Online (Sandbox Code Playgroud)

然后在save_post钩子中使用这样的函数:

log_it($_POST);
log_it('The value for ' . $custom_field . ' is ' . $_POST[$custom_field]);
Run Code Online (Sandbox Code Playgroud)

确保wp-content/debug.log是可写的,并且您已在wp-config.php中启用了调试:

@ini_set('display_errors',0);
define( 'WP_DEBUG',         true );  // Turn debugging ON
define( 'WP_DEBUG_DISPLAY', false ); // Turn forced display OFF
define( 'WP_DEBUG_LOG',     true );  // Turn logging to wp-content/debug.log ON
define( 'WP_POST_REVISIONS', false); // Disables revision functionality
Run Code Online (Sandbox Code Playgroud)


T.T*_*dua 5

方法一:

if (isset($_POST)) die(print_r($_POST)); //answer by Tumas
Run Code Online (Sandbox Code Playgroud)


方法二:

在文件夹中创建日志文件 ( my_logs.txt ),您可以在其中使用以下代码:

add_action('save_post', 'something_process',11,11);
function something_process() 
{
    print_r($_POST);
    $tmp = fopen(dirname(__file__).'/my_logs.txt', "a+"); fwrite($tmp,"\r\n\r\n".ob_get_contents());fclose($tmp);
}
Run Code Online (Sandbox Code Playgroud)