Debug output during filter execution doesn’t work

I’m working on my first WordPress plugin and am working on my first Custom Post Type. I’m looking to provide some debug output so I can see what kind of data I have to work with.

I have the following code (some snipped for size):

Read More
class MyPlugin_Admin {

    ... SNIP ...

    private function __construct() {
        ... SNIP ...
        add_filter( 'wp_insert_post_data', array($this, 'adjust_cpt_data'), 10, 2);
    }

    public function adjust_cpt_data($data, $postarr) {
        var_dump($data);
        echo "###########################################";
        echo "###########################################";
        echo "###########################################";
        echo "###########################################";
        echo "###########################################";
        echo "###########################################";
        echo "###########################################";
        echo "###########################################";
        echo "###########################################";
        echo "###########################################";
        echo "###########################################";
        echo "###########################################";
        //d($data);
        //dd($postarr);
        return $data;
    }

    ... SNIP ...

}

I’ve tried using all kinds of different outputs. The d() and dd() functions are from Debug Bar and Kint Debugger. They don’t work either. If I add some debugging code right under add_filter() that works properly, but not inside adjust_cpt_data(). There is nothing I can do in this function to output data to the screen.

I have been able to verify that this code is running, because if I remove the “return $data;” line, the custom post data does not get updated properly, it just refreshes with the old data.

Have anyone provide any insight on why this is and how I can print some debugging output to the screen.

As a side note, I realize I can get the contents of $data and $postarr by looking at the WP Codex, but I will inevitably run into this issue again when I need to debug my own code.

Related posts

1 comment

  1. You need to kill the script. Add die; after your var_dump.

    WordPress post submissions operate on a post/redirect/get principle. The reason you don’t see the output is that the page has been reloaded. That is, you post the form, the form gets processed, and the page is redirected back to itself but without the $_POST data. It helps prevent “double-submissions”.

    You have to kill the script to prevent the redirect or the page you end up seeing is not the page with the output you are expecting to see.

    You should be able to log errors to a file if you use error_log or look into some more complicated debugging tools.

Comments are closed.