0

I'm encountering something weird.

I made a function on functions.php that I want to trigger when a post is saved or updated.

function gh_set_inner_project_order($post_id){

  $parent_id = wp_get_post_parent_id( $post_id );

  var_dump($parent_id);
  if($parent_id == 4){
    $page_projects = gh_get_inner_project_order($post_id);
    $i = 0;
    foreach ($page_projects as $project_id) {
      update_field('inner_order', $i, $project_id);
      $i++;
    }
  }

}

And then, simply :

add_action('save_post', 'gh_set_inner_project_order');

On my localhost, it works fine. The function is triggeredwhen I save a post or when I update it, it logs the $parent_id.

But on the distant server, it only works when I create a new post / page. I really can't figure out why.

Any idea?

EDIT:

Just to be sure, I modified my function so it's really minimal now:

function gh_set_inner_project_order(){


  var_dump('---------- gh_set_inner_project_order ---------');


}

But still, it doesn't work when I update a post on the distant server (no issue on my localhost).

  • On local server, it returns the expected ID when I publish a post or when I update it, but on distant server, it only works when I create a new post, not on update. – enguerranws May 7 '16 at 8:54
  • Actually, it does! So it's triggered, but the var_dump() wasn't displayed. Is it about var_dump() and distant server PHP conf (I'm not expert in that kind of stuff)? – enguerranws May 7 '16 at 9:04
  • A var_dump() in save_post action doesn't display any information on the screen. If you need to debug save_post action, you can follow this example. But you already know it is triggered, the only problem I see in your code is the hardcoded $parent_id == 4; it is very probably that the parent id is not always 4. – cybmeta May 7 '16 at 9:09
  • @cybmeta I think that's not true! It displays the output but may be hidden behind admin bar! – Sumit May 7 '16 at 9:14
  • 1
    Okay sorry @cybmeta I just completely forgot about redirection after saving post. I was testing suing var_dump('called'); die; – Sumit May 7 '16 at 9:42
1

A var_dump() in save_post action doesn't display any information on the screen. Well, it does only when you are on "Add new" screen. This is due the different sequence of actions that follow when creating a new post or when editing it.

When you click on "publish" or "update" button, there is a request, the post is saved, save_post action is triggered and then you are redirected to the edit screen.

The redirection implies a new request and this new request doesn't trigger the save_post action, so you can not see any information printed just becasue that action is NOT triggered to generate the screen you are seeing.

If you need to debug save_post action, you can follow this example (it needs WP_DEBUG on and stores the information in the error log file in your server, see error_log() PHP function):

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 );
     }
   }
 }
}

And then, in the save_post callback:

add_action('save_post', 'gh_set_inner_project_order');
function gh_set_inner_project_order() {
    log_it( $parent_id );
    // .... 
}

Remember that you can pass a custom error log file to error_log() function if you want to log the information in a different file from server log. See error_log() docs for more information. For example:

error_log( $var_to_log, 3 , __DIR__ . "/my-log.txt" );
  • $parent_id == 4 is made on purpose : I want to check if the current post is a child a of specific page. – enguerranws May 7 '16 at 9:13
  • If that value, 4, is correct and the action is triggered, what is your problem then? I don't get you. Are you sure the parent ID is 4 in local and remote server? – cybmeta May 7 '16 at 9:14
  • Read the question: I didn't know that the action was triggered, as there was no var_dump() on distant server when I was updating a post. Note that when I was creating a post on the distant server, I did see the var_dump() value. – enguerranws May 7 '16 at 9:17
  • 1
    Mmm ok, that sounds actually a better way to debug this kind of actions, thanks. – enguerranws May 7 '16 at 9:35
  • 1
    @Sumit I added a explanation of the redirection, maybe you read the answer before I did it. – cybmeta May 7 '16 at 10:48

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.