How to debug save_post actions in WordPress?

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:

Read More
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?

Related posts

Leave a Reply

6 comments

  1. 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
    
  2. 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,"rnrn".ob_get_contents());fclose($tmp);
    }
    
  3. 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

  4. 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' );
    }