1

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):

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.

  • You need to kill the script. Add die; after your var_dump. – s_ha_dum Jan 25 '14 at 21:35
  • That DOES work. Is there not a way to have it print this stuff in line? What is preventing this output from being displayed? – Caesar Kabalan Jan 25 '14 at 21:40
2

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.

  • This makes perfect sense. I'll mark this as the answer since it gives a solution and also an excellent explanation about why WordPress works as it does. Thank you! – Caesar Kabalan Jan 25 '14 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.