如何在 WordPress 中调试 save_post 操作?

发布于 2024-12-11 12:11:55 字数 380 浏览 0 评论 0原文

我正在生成一些自定义帖子元,并准备添加到帖子的元中。我知道该怎么做。但是,save_post 会在发送 POST 数据后导致重定向。这意味着我被重定向到仪表板并无法访问我的 POST 数据 - 因此我无法轻松调试。

目前我正在使用类似的东西:

add_action('save_post', 'something_process');

function something_process() {
   if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
       return;
   print_r($_POST);
}

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

I have some custom post meta being generated and am ready to add to a post's meta. I know how to do this. However, save_post causes a redirection after POST data has been sent. This means I am redirected to the dashboard and lose access to my POST data - therefore I cannot debug easily.

Currently I am using something like:

add_action('save_post', 'something_process');

function something_process() {
   if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
       return;
   print_r($_POST);
}

Is there a way to easily debug this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

最偏执的依靠 2024-12-18 12:11:55

对我来说最好的方法是使用一个函数将值记录到 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 );
     }
   }
 }
}

然后在 save_post 挂钩中使用如下函数:

log_it($_POST);
log_it('The value for ' . $custom_field . ' is ' . $_POST[$custom_field]);

确保 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

The best approach for me has been to use a function to log the values to wp-content/debug.log, lifted from 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 );
     }
   }
 }
}

Then use the function like this in your save_post hook:

log_it($_POST);
log_it('The value for ' . $custom_field . ' is ' . $_POST[$custom_field]);

Make sure that wp-content/debug.log is writable, and that you have debugging enabled in 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
硬不硬你别怂 2024-12-18 12:11:55

方法 1:

if (isset($_POST)) die(print_r($_POST)); //answer by Tumas

方法 2:

在使用此代码的文件夹中创建日志文件 (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);
}

Method 1:

if (isset($_POST)) die(print_r($_POST)); //answer by Tumas

Method 2:

create log file (my_logs.txt) in a folder, where you use this code:

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);
}
落花浅忆 2024-12-18 12:11:55

到目前为止,我发现的最佳解决方案是将 $_POST 存储在会话变量中以供稍后访问。

The best solution I've found so far is storing the $_POST in a session var for access later.

娇女薄笑 2024-12-18 12:11:55

第一种方法:

die(print_r($post_id));

第二种方法:

var_dump($post_id);

第三种方法:

<?php
  echo <pre>{whatever you want to echo goes here}</pre>
?>

或者使用任何浏览器插件进行控制台日志记录

可能会有所帮助..祝你好运

First Approach:

die(print_r($post_id));

Second Approach:

var_dump($post_id);

Third Approach:

<?php
  echo <pre>{whatever you want to echo goes here}</pre>
?>

Or take any browser add-ons for console logging

may one of three help..Good Luck

许你一世情深 2024-12-18 12:11:55

您还可以将调试消息保存在 WordPress 选项中,并在重定向后将其显示为管理消息。

// display all notices after saving post
add_action( 'admin_notices', 'show_admin_notices', 0 );

// your custom save_post aciton
add_action( 'save_post', 'custom_save_post' );

function custom_save_post() {
    store_error_in_notices_option( 'my debug message' );
}

function store_error_in_notices_option( $m ) {
    if ( ! empty( $m ) ) {
        // store error notice in option array
        $notices = get_option( 'my_error_notices' );
        $notices[] = $m;
        update_option( 'my_error_notices', $notices );
    }
}

function show_admin_notices() {
    $notices = get_option( 'my_error_notices' );
    if ( empty( $notices ) ) {
        return;
    }
    // print all messages
    foreach ( $notices as $key => $m ) {
        echo '<div class="error"><p>' . $m . '</p></div>';
    }

    delete_option( 'my_error_notices' );
}

You could also save your debug messages in a WordPress option and show it as an admin message after the redirect.

// display all notices after saving post
add_action( 'admin_notices', 'show_admin_notices', 0 );

// your custom save_post aciton
add_action( 'save_post', 'custom_save_post' );

function custom_save_post() {
    store_error_in_notices_option( 'my debug message' );
}

function store_error_in_notices_option( $m ) {
    if ( ! empty( $m ) ) {
        // store error notice in option array
        $notices = get_option( 'my_error_notices' );
        $notices[] = $m;
        update_option( 'my_error_notices', $notices );
    }
}

function show_admin_notices() {
    $notices = get_option( 'my_error_notices' );
    if ( empty( $notices ) ) {
        return;
    }
    // print all messages
    foreach ( $notices as $key => $m ) {
        echo '<div class="error"><p>' . $m . '</p></div>';
    }

    delete_option( 'my_error_notices' );
}
懒的傷心 2024-12-18 12:11:55

我用它来快速格式化输出:

die( '<pre>' . print_r( $_POST, true ) . '</pre>');

I use this for a quick formatted output :

die( '<pre>' . print_r( $_POST, true ) . '</pre>');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文