2

I was doing some action on save_post hook, and noticed that there exists

wp_is_post_autosave( $post_id ) 

function.

How does it differ from DOING_AUTOSAVE constant?

If I want my action not to run on autosave should I do both

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  return;
}

and

if ( \wp_is_post_autosave( $post_id ) ) {
  return;
}

Or is one enough?

The DOING_AUTOSAVE constant gets defined in wp-admin/includes/post.php, wp-includes/class-wp-customize-manager.php and wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php, whereas wp_is_post_autosave() checks if the post is revision and has autosave in it's name.

I'm a bit confused what to use, and if it's good idea to use both.

  • 1
    If you want to check if a post is an auto-save, you should use the function; but if you want to check if WordPress is doing an auto-save, then you would check the constant. – Sally CJ Jan 31 at 11:49
  • But if WordPress is doing an autosave, the post type of that post would be auto save (or revision), no? Is it wrong to use both? – dingo_d Jan 31 at 11:53
  • No, it's not wrong to use both. But normally, checking just the constant would do it in your case - i.e. if the constant is defined and a true, you can "exit" your callback function (cancel its action). If you only want to cancel your callback action if the post has been successfully auto-saved, then you would use the function. – Sally CJ Jan 31 at 12:14
  • No problem. And sorry, I didn't have a chance to correct the "the function" which should be "the function as well" - example. – Sally CJ Jan 31 at 13:20

Your Answer

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

Browse other questions tagged or ask your own question.